Multi-Dimensional Arrays(3D Arrays) in programming

Multi-Dimensional Arrays(3D Arrays) in programming

·

4 min read

Table of contents

No heading

No headings in the article.

3D Array in Java

Java allows for arrays of two or more dimensions. a two-dimensional (2D) array is an array of arrays. A three-dimensional (3D) array is an array of arrays.

In programming, an array can have two, three, or even ten or more dimensions. The maximum dimensions a Java program can have depends on which compiler is being used.

More dimensions in an array mean more data to be held, but also means greater difficulty in managing and understanding arrays.

How to Declare a Multidimensional Array in Java

A multidimensional array is declared using the following syntax:

data_type[d1][d2][]..[dn] array_name = new data_type[size1][size2]….[sizeN]; Where:

  • data_type: Type of data to be stored in the array. for example: int,char,etc.
  • d: The dimension of the array created. For example: 1D,2D, etc.
  • Array_name: Name of the array
  • size1,size2..sizeN: Sizes to the dimensions respectively.

Examples:

(1) Two-dimensional array:

 int[][] twoD_arr = new int[10][20];

(2) Three-dimensional array:

float[][][] threeD_arr = new float[10][20][30];

In Example 1:

  • int designates the array type integer.
  • twoD_arr is the name of our 2D array.
  • Our array can hold 200 integer-type elements. This number is reached by multiplying the value of each dimension. In this case:10x20=200.

In Example 2:

  • Array threeD_arr is a three-dimensional array.
  • It can hold 6000 floating-point elements (10x20x30=4500).

can you see the power of declaring an array over variables? When it comes to holding multiple values in Java programming, we would need to declare several variables. But a single array can hold thousands of values.

Note: For the sake of simplicity, this tutorial discusses 3D arrays only. Once you grab the logic of how the 3D array works, then you can handle 4D arrays and larger ones.

Here is a visual representation of multi Dimensional array in memory

1_tRxtJ50d84yK0EkqeX0nmg.png

Explanation of a 3D array:

Let's take a closer look at a 3D array. A 3D array is essentially an array of arrays an array or a collection of 2D arrays and a 2D array is an array of 1D array. A three-dimensional array is a complex form of a multidimensional array.

it may sound a bit confusing, but don't worry. As you practice working with multidimensional arrays, you start to grasp the logic.

The diagram below may help you understand:

3D-array.jpg

Initializing a 3D array in Java Like any other variable or array, a 3D array can be initialized during compilation. By default, in Java, an uninitialized 3D array contains "0" values, not valid for the intended use.

Let's see a complete example of how to initialize a 3D array:

Declaration and initialization of 3D array

Declaration-Syntax:

data_type[][][] array_name - new data_typw[x][y][z];

Initialization-Syntax:

array_name[array_index][row_index][column_index]=value;

Direct Method of Declaration: Syntax:

data_type[][][] array_name = {{ {Val, Val,...}, {Val, Val,...} }, { {Val, Val,...}, {Val, Val,...} } };

Example:

int[][][] arr = {
        {
                {1,2,3},
                {4,5,6},
                {7,8,9}
         },
         {
                {10,11,12},
                {13,14,15},
                {16,17,18}
          },
          {
                {19,20,21},
                {22,23,24},
                {25,26,27}
            };

In the code above, we have declared a multidimensional integer array named "arr" which can hold 3x3x3 (or 27) elements. We have also initialized the multidimensional array with some integer values. As I said earlier, a 3D array is an array of 2D arrays. I have divided elements accordingly for easy understanding. Looking at the Java code sample above,

To call values from the array, imagine the 3D array above as a collection table. Each nested bracket cluster is a table with rows and columns. To access or store any element in a 3D array, you need to know its table number, row number, and column number.

An example: You need to access value 25 from the above 3D array. So, first, check the table: in this case, 25 is in table 2 (remember: tables, rows, and columns are counted starting at 0, so the second table is table 1.) Once you find the table number, check which row of that table has the value, and then check the column number. So applying the above logic 25 is located in table 2, row 2, and column 0, hence the address is arr[2][2][0]. Print this address and you will get the output:25.

Jagged Array in Java:

A jagged array is an array of arrays such that member arrays can be of different sizes, i.e., We can create a 2D array but with a variable number of columns in each row. These types of arrays are also known as jagged arrays.

Pictorial representation of jagged array :

Representation-of-a-jagged-array.png

Declaration and initialization of jagged array:

    data_type array_name [][] = new data_type[n][];//n: no. of rows
    array_name[] = new data_type[n1];//n1 = no. of columns in row 1
    array_name[] = new data_type[n2];//n2 = no. of columns in row 2
    array_name[] = new data_type[n3];//n3 = no. of columns in row 3
                        .
                        .
                        .
    array_name[] = new data_type[nk];//nk = no. of columns in row k

Alternative, ways to initialize a jagged array:

int arr[][] = new int[][] {
                new int[] {10,20,30,40},
                new int[] {50,60,70,80,90,100},
                new int[] {110,120},
                };
                //or
int arr[][] =  {
               new int[] {10,20,30,40},
               new int[] {50,60,70,80,90,100},
               new int[] {110,120},
            };
                //or
int arr[][] = new int[][] {
                   {10,20,30,40},
                   {50,60,70,80,90,100},
                   {110,120},
                   };

Now see how to Output a jagged array Syntax:

for(int i=0;i<arr.length;i++){
      for(int j=0;j<arr[i].length;j++){
             System.out.print(arr[i][j]+"  ");
       }
       System.out.println();
}

Hope this helps. Thanks for reading