2-D Arrays
A 2-dimensional array is actually an array of 1-dimensional arrays. All functions and methods are exactly the same as 1-dimensional arrays.
Here is how to create a 2-dimensional array followed by an explanation:
int [][] grid=new int [4][5];
// int means that the type is an int array
// [][] indicates the number of dimensions, in this case, it is 2-dimensional
// grid is the name used to reference the array
// int [] grid declares the reference to the object
// the new function creates the actual object
// int is again, the type
// [4][5] specifies the size of the dimensions
The following is a visual representation of the newly created array:
You may also think of it as:
The shaded square is represented by grid[2][3].
Note: The index of the first row and column is 0, meaning the index of the last row and column is 1 less than the specified size. Also in order to deal with 2-dimensional arrays, nested loops are almost always involved.
For example, to fill each element of grid efficiently with the value 5, you would do it in the following way:
Example
public class NestedLoops
{
public static void main(String[] args)
{
int[][] grid = new int[4][5];
//below are two loops, one nested inside another
//row and col represent the rows and columns of the array
for (int row = 0; row < 4; row++)
{
{
for (int col = 0; col < 5; col++)
{
grid[row][col] = 5;
System.out.print(grid[row][col] + " ");
}
System.out.println();
}
}
}
}
System Output
5 5 5 5 5
5 5 5 5 5
5 5 5 5 5
5 5 5 5 5
Passing 2-D Arrays
Passing 2-dimensional or multi-dimensional arrays are the same as passing 1-dimensional arrays. Take a look at the following example:
File: ArrayTotal.java
public class ArrayTotal
{
public static void main(String[]args)
{
int row=3;
int col=5;
int[][]grid=new int [row][col]; //creating a new array
//the following line calls the below method and passes the above
array with grid in the parameters
fillArray(grid, row, col, 7);
}
//method: fillArray
//purpose: to fill the 2-D array with a given value
//parameters: the array, row and column, and value to fill
//return: none
public static void fillArray(int[][]a, int r, int c, int value)
//in the method definition above, notice how the array is passed
through parameters
{
for (int i= 0; i < r; i++)
{
{
for (int j = 0; j<c; j++)
{
a[i][j] = value;
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
}
Cloning 2-D Arrays
A 2-dimensional array is really an array of 1-dimensional arrays. Therefore, cloning 2-dimensional arrays simply means cloning all the individual 1-dimensional arrays. Take a look at the following example:
File: ArrayTotal.java
public class Clone2DArray
{
public static void main(String[] args)
{
int row = 3;
int col = 5;
int[][] grid = new int[row][col]; //creates the 2-d array
//the following lines fill and print the 2-d array
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
{
grid[i][j] = i;
System.out.print(grid[i][j]);
}
System.out.println();
}
//the following lines clone the 2-d array
int[][] copy = grid.clone(); //makes a shallow copy
for (int i = 0; i < 3; i++)
for (int j = 0; j < 5; j++)
//the following line clones individual references, making a
deep copy
copy[i][j] = grid[i][j];
//the following lines print the 2-d array
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
System.out.print(copy[i][j]);
System.out.println();
}
}
}
System Output
00000
11111
22222
00000
11111
22222
Multi-Dimensional Arrays
Multi-dimensional arrays work in the same way as 2-dimensional arrays. They are declared in the following way:
int[][][] grid=new int [11][11][11]; //a 3-dimensional array
char [][][][] element=new char [1][1][10][100]; //a 4-dimensional array
boolean[][][][][] space=new boolean[1][2][3][4][5];
//a 5-dimensional array (above)
...
With multi-dimensional arrays, you cannot visually imagine them, but it is definitely possible to create them in programming.
Related Links
For more information on multidimensional arrays, check out these useful links:
|
|
 |
2-D Arrays |
Willamette University's online lesson of 2-dimensional arrays. |
|
|