Multi Dimensional Array

Multi Dimensional Array in Java:

In Java, a multidimensional array is an array that contains other arrays as its elements. This allows you to organize data in a grid-like structure, with multiple dimensions. A two-dimensional array is the most common type of multidimensional array, but you can create arrays with more than two dimensions as well. Multidimensional arrays are often used to represent matrices, tables, images, and other structured data.

Here’s the basic concept of a multidimensional array in Java:

  • A two-dimensional array is an array of arrays. Each element of the main array is itself an array.
  • The first dimension represents rows, and the second dimension represents columns.
  • The elements in a multidimensional array are accessed using indices for each dimension.

Syntax for creating a two-dimensional array:

dataType[][] arrayName = new dataType[rows][columns];

  • dataType: The array’s elemental data types.
  • arrayName: The name of the array.
  • rows: The number of rows in the array.
  • columns: The number of columns in the array.
Example of a 2D array:
public class MultidimensionalArrayExample {
    public static void main(String[] args) {
        // Creating a 2D array (3x3 matrix)
        int[][] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };

        // Accessing elements
        System.out.println(matrix[0][0]); // Prints the element at row 0, column 0 (1)
        System.out.println(matrix[1][2]); // Prints the element at row 1, column 2 (6)
    }
}

Explanation:

  1. public class MultidimensionalArrayExample : This line defines the start of a Java class named MultidimensionalArrayExample.
  2. public static void main(String[] args) : This is the primary technique that acts as the program’s entry point. Its parameter is an array of strings (args).
  3. int[][] matrix = {…};: This line declares and initializes a two-dimensional array named matrix. The array has three rows and three columns, forming a 3×3 matrix. The curly braces {} contain the values for each row.
  4. System.out.println(matrix[0][0]);: This line prints the element at the first row (row 0) and the first column (column 0) of the matrix array. In Java, array indices start from 0, so matrix[0][0] accesses the element at the top-left corner of the matrix, which is 1.
  5. System.out.println(matrix[1][2]);: This line prints the element at the second row (row 1) and the third column (column 2) of the matrix array. The value at this position is 6.

Output:

1

6

 

Let’s come up with a different array example:

public class MultidimensionalArrayExample {

    public static void main(String[] args) {

        int ages[][];
        ages = new int[4][3];
        ages[0][0] = 34;
        ages[0][1] = 56;
        ages[0][2] = 71;
        ages[1][0] = 67;
        ages[1][1] = 63;
        ages[1][2] = 70;
        ages[2][0] = 23;
        ages[2][1] = 29;
        ages[2][2] = 31;
        ages[3][0] = 17;
        ages[3][1] = 43;
        ages[3][2] = 19;
        // outer loop
        for (int r = 0; r <= 3; r++) {
            // inner loop
            for (int c = 0; c <= 2; c++)
            System.out.print(ages[r][c]);
            System.out.println();
        }
        
    }
}

Explanation:

  1. public class MultidimensionalArrayExample: This line defines the start of a Java class named MultidimensionalArrayExample .
  2. public static void main(String[] args) : This is the primary technique that acts as the program’s entry point. Its parameter is an array of strings (args).
  3. int ages[][];: Declares a 2D array variable named ages without initializing it.
  4. ages = new int[4][3];: Initializes the ages array with dimensions 4×3, creating a 2D array capable of holding 4 rows and 3 columns.
  5. Subsequent lines set specific values for each element in the ages array, populating it with ages.
  6. for (int r = 0; r <= 3; r++) : Begins an outer loop that iterates over the rows (ranging from 0 to 3).
  7. for (int c = 0; c <= 2; c++): Starts an inner loop that iterates over the columns (ranging from 0 to 2).
  8. System.out.print(ages[r][c] + ” “);: Prints the value of the element at row r and column c, followed by a space.
  9. System.out.println();: Prints a newline to move to the next row after each inner loop iteration.

Output:

345671

676370

232931

174319

We can also use the for…each loop to access elements of the multidimensional array. For example,

public class Main {

    public static void main(String[] args) {

        // create a 2d array
        int[][] a = { { 1, -2, 3 }, { -4, -5, 6, 9 }, { 7 }, };

        // first for...each loop access the individual array
        // inside the 2d array
        for (int[] innerArray : a) {
            // the second for...each loop accesses every row element
            for (int data : innerArray) {
                System.out.print(data);
            }
            System.out.println();
        }

    }
}

Explanation:

  1. public class MultidimensionalArrayExample: This line defines the start of a Java class named MultidimensionalArrayExample .
  2. public static void main(String[] args) : This is the primary technique that acts as the program’s entry point. Its parameter is an array of strings (args).
  3. int[][] a = {…};: This line declares and initializes a 2D array named a. The array has three rows, each represented as an array of integers. The first row contains elements {1, -2, 3}, the second row contains elements { -4, -5, 6, 9 }, and the third row contains a single element {7}.
  4. for (int[] innerArray : a) {: This starts the first for-each loop. It iterates over each inner array (row) within the 2D array a.
  5. for (int data : innerArray) {: This starts the second for-each loop. It iterates over each element data inside the current inner array (row).
  6. System.out.print(data);: This prints the value of the current element data without moving to the next line.
  7. System.out.println();: This prints a newline character to move to the next line after all elements of the current inner array (row) have been printed.

Output:

1-23

-4-569

7

 

Visited 1 times, 1 visit(s) today

Comments are closed.

Close