Syntax for creating a two-dimensional array in Java - Stack Overflow
Need help in Java specifically 2D Array
In desperate need to master 2 dimensional arrays
Finding neighbors in a 2D array
a while back i wrote a simple Game of Life program( probably not the best program out there). however, it uses the same 8 cell check around each cell. i used nested for loops to deal with it. the snippet below starts at line 121 of the program.
private void age() {
generation++;
//loop through all cells
for ( int row = 0; row < rows; row++) {
for ( int col = 0; col < cols; col++) {
processCell( row, col);
}
}
parent.updateGeneration( generation);
}
private void processCell( int row, int col) {
int alive = 0;
//start at top left cell, once cell away
for ( int startRow = row == 0 ? 0 : row - 1; startRow < row + 2 && startRow < rows; startRow++) {
for ( int startCol = col == 0 ? 0 : col - 1; startCol < col + 2 && startCol < cols; startCol++) {
if ( cells[ startRow][ startCol].getGeneration() == generation) {
if ( cells[ startRow][ startCol].wasAlive()) {
alive++;
}
} else if ( cells[ startRow][ startCol].isAlive()) {
alive++;
}
}
}
if ( cells[ row][ col].isAlive()) {
alive--;
}
if ( cells[ row][ col].isAlive() && (alive <= 1 || alive >= 4)) {
changeCell( cells[ row][ col], false);
} else if ( !cells[ row][ col].isAlive() && alive == 3) {
changeCell( cells[ row][ col], true);
}
} it might be helpful if u run the code in debug and see how it runs. if u need more help replay back to this and i try my best to help u, good luck.
More on reddit.comHow to declare a 2D array in Java?
What is a common use case for 2D arrays in Java?
How to create a 2D array with predefined values in Java?
Videos
Try the following:
int[][] multi = new int[5][10];
... which is a short hand for something like this:
int[][] multi = new int[5][];
multi[0] = new int[10];
multi[1] = new int[10];
multi[2] = new int[10];
multi[3] = new int[10];
multi[4] = new int[10];
Note that every element will be initialized to the default value for int, 0, so the above are also equivalent to:
int[][] multi = new int[][] {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
... or, more succinctly,
int[][] multi = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
We can declare a two dimensional array and directly store elements at the time of its declaration as:
int marks[][]={{50,60,55,67,70},{62,65,70,70,81},{72,66,77,80,69}};
Here int represents integer type elements stored into the array and the array name is 'marks'. int is the datatype for all the elements represented inside the "{" and "}" braces because an array is a collection of elements having the same data type.
Coming back to our statement written above: each row of elements should be written inside the curly braces. The rows and the elements in each row should be separated by a commas.
Now observe the statement: you can get there are 3 rows and 5 columns, so the JVM creates 3 * 5 = 15 blocks of memory. These blocks can be individually referred ta as:
marks[0][0] marks[0][1] marks[0][2] marks[0][3] marks[0][4]
marks[1][0] marks[1][1] marks[1][2] marks[1][3] marks[1][4]
marks[2][0] marks[2][1] marks[2][2] marks[2][3] marks[2][4]
NOTE:
If you want to store n elements then the array index starts from zero and ends at n-1.
Another way of creating a two dimensional array is by declaring the array first and then allotting memory for it by using new operator.
int marks[][]; // declare marks array
marks = new int[3][5]; // allocate memory for storing 15 elements
By combining the above two we can write:
int marks[][] = new int[3][5];