0

I've run across a very perturbing problem. I'm trying to create a "chunking" system for this little game I'm making. What I want to do right now is just generate infinite chunks of dirt blocks. It works right now, but I'm just using a two dimensional array, which stores the column and the index of the dirt. Here is the actual code:

public void generate(){
    int newMax = dirtCount + dirtGeneratorChunk;
    int oldCount = dirtCount;
    for(int i = oldCount;i<newMax;i++){
        dirt[dirtCount] = newDirt(spawnDirtX+column*16,
                    spawnDirtY,16,16,column,columnNumber);
        columnNumber ++;
        blockGrid[column][columnNumber] = spawnDirtY;
        updateBlockGrid(column,(spawnDirtY-spawnDirtTop)/16,spawnDirtY);
        dirtX[i] = spawnDirtX+column*16;
        dirtY[i] = spawnDirtY;
        hasDirt = true;
        dirtCount ++;
        spawnDirtY += 16;
        if(spawnDirtY>500){
            column++;
            columnNumber = 0;
            spawnDirtY = spawnDirtYOriginal;
        }
    }
    repaint();
}

Hopefully that is helpful to show what I'm trying to do. As I'm sure you can tell by the code, I'm pretty new to Java and may be using bad practices/inefficient methods. If you have any suggestions on how I can improve my programming, I would love to hear them! Back to the point. Here is what I have so far on the ArrayList:

//Construct a new empty ArrayList for type int[][]
    ArrayList<int[][]> chunkHolder = new ArrayList<int[][]>();
    //Fill ArrayList (index, int[][])
    chunkHolder.add(chunkCount, createBlockGrid());
    chunkCount ++;
    Object[] chunkArray = chunkHolder.toArray();
    System.out.println(chunkArray[0]);

    public static int[][] createBlockGrid(){
        int[][] blockGrid = new int[64][256];
        return blockGrid;
    }

When I attempt to use System.out.println(chunkHolder.get(0));, it returns: [[I@337838. Obviously this isn't what I want... what am I doing wrong? Thank you all for your help! ~Guad (If I explained anything without clarity, please tell me!)

2
  • 1
    Its printing using the toString() method of Object. You could probably use Arrays.toString(chunkHolder.get(0)) method from the java.util.Arrays package, although I've never used it on 2D arrays. Check out this post: stackoverflow.com/questions/409784/… Commented Mar 28, 2014 at 15:44
  • int[][] firstChunk = chunkHolder.get(0); Now you can interact with firstChunk as you normally would. Try System.out.println(firstChunk[0][0]); for instance. Also, try printing blockGrid after you initialize it, you'll see why you get that representation - that's just what it looks like to print an array. Commented Mar 28, 2014 at 15:57

1 Answer 1

3

Your ArrayList holds int[][] elements, that is two dimensional integer arrays. By retrieving the first element using chunkHolder.get(0), you get a two dimensional integer array. These things, if printed, produce something like what you got. If you wanted the array elements, you would

  1. Retrieve an array element (like you did)
  2. Retrieve the elements of the array

As suggested, you might want to use (from Java 5) Arrays.deepToString(arr):

System.out.println(Arrays.deepToString(chunkHolder.get(0)));

Otherwise, you can loop over the array to print it out. For example:

int[][] a = chunkHolder.get(0);
for (int[] b : a) {
 for (int c : b) {
  System.out.print(c + " ");
 }
 System.out.println();
}
Sign up to request clarification or add additional context in comments.

3 Comments

The result of get is going to be a two-dimensional integer array, not a regular integer array.
I noticed that it might have been a bit badly worded. Corrected.
Thank you, that solved my problem! I've still got to get the actual chunks working, but it wouldn't be fun if I asked questions on everything! Thanks for the detailed answer, it helped me understand more about Java!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.