How to Create ArrayList of 2D Arrays

htmlhigh5

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!)

blueygh2

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();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related