JAVA MULTIDIMENSIONAL ARRAYS |
In Java, an array is a data structure that can store multiple values of the same data type. A multidimensional array, also known as a nested array, is an array that contains arrays within it. In other words, it's an array of arrays. This type of data structure can be useful for many purposes, including representing tables, graphs, and more.
There are two types of multidimensional arrays in java:
1) 2D Array:
A 2D array, also known as a two-dimensional array, is an array that contains arrays within it. In other words, it's an array of arrays. This type of data structure can be useful for many purposes, including representing tables, graphs, and more.Declaring a 2D Array:
There are three ways to declare a 2D array in Java:
- Using the new operator:
int[][] arrayName = new int[rowCount][columnCount];
- Using an array literal:
int[][] arrayName = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
- Using a loop:
int[][] arrayName = new int[rowCount][columnCount]; for (int i = 0; i < rowCount; i++) { for (int j = 0; j < columnCount; j++) { arrayName[i][j] = someValue; } }
Note: No matter which method you choose, the result will be a 2D
array with the specified dimensions and initial values.
Accessing Elements of a 2D Array:
To access an element in a 2D array, you need to specify two indices, one for the row and one for the column.For example:
int element = arrayName[rowIndex][columnIndex];
Iterating Over a 2D Array:
To loop through a 2D array, you can use nested for loops. The outer loop will iterate over the rows, and the inner loop will iterate over the columns.Here's an example:
for (int i = 0; i < arrayName.length; i++) { for (int j = 0; j < arrayName[i].length; j++) { System.out.print(arrayName[i][j] + " "); } System.out.println(); }
In this example, the outer loop iterates over the rows, and the inner loop iterates over the columns. The arrayName[i].length expression returns the number of columns in the row i. The System.out.print statement prints the current element, followed by a space. The System.out.println statement prints a newline character after each row has been printed.
2) 3D Array:
In Java, a 3D array is an array of arrays of arrays. It's a data structure that can store multiple values of the same data type, organized into a series of two-dimensional arrays. 3D arrays can be useful for representing data in a 3-dimensional space, such as a cube, a sphere, or any other object with a third dimension.Declaring a 3D Array:
There are three ways to declare a 3D array in Java:
- Using the new operator:
int[][][] arrayName = new int[depthCount][rowCount][columnCount];
In this method, you specify the number of depths, rows, and columns in the array, and Java will create a 3D array with the specified dimensions. By default, the elements of the array will be set to their default values (0 for numeric types, null for reference types).
- Using an array literal:
int[][][] arrayName = {{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, {{10, 11, 12}, {13, 14, 15}, {16, 17, 18}}};
In this method, you provide the values for each element of the array directly in the declaration. The number of depths, rows, and columns is determined by the number of values provided.
- Using a loop:
int[][][] arrayName = new int[depthCount][rowCount][columnCount]; for (int i = 0; i < depthCount; i++) { for (int j = 0; j < rowCount; j++) { for (int k = 0; k < columnCount; k++) { arrayName[i][j][k] = someValue; } } }
In this method, you create the array using the new operator and then use nested loops to initialize the values of each element. The outermost loop iterates over the depths, the middle loop iterates over the rows, and the innermost loop iterates over the columns. The code sets the value of each element to someValue.
Note: No matter which method you choose, the result will be a 3D array with the specified dimensions and initial values.
Accessing Elements of a 3D Array:
To access an element in a 3D array, you need to specify three indices, one for each dimension:int element = arrayName[depthIndex][rowIndex][columnIndex];
Iterating Over a 3D Array:
To loop through a 3D array, you can use nested for loops. The outermost loop will iterate over the depths, the middle loop will iterate over the rows, and the innermost loop will iterate over the columns. Here's an example:for (int i = 0; i < arrayName.length; i++) { for (int j = 0; j < arrayName[i].length; j++) { for (int k = 0; k < arrayName[i][j].length; k++) { System.out.print(arrayName[i][j][k] + " "); } System.out.println(); } System.out.println(); }
In this example, the outermost loop iterates over the depths, the middle loop iterates over the rows, and the innermost loop iterates over the columns. The arrayName[i].length expression returns the number of rows in the depth i, and the arrayName[i][j].length expression returns the number of columns in the row j. The System.out.print statement prints the current element, followed by a space. The System.out.println statement prints a newline character after each row and depth has been printed.