Summary: In this tutorial, you will learn to about multidimensional array in Java. You will learn to create,access and modify 2d and jagged array in Java.

Multidimensional Array in Java

In Java, a multidimensional array is an array that contains one or more arrays as elements. There are two types of multidimensional arrays:

1. Two-dimensional arrays

These are arrays that contain a list of rows and columns. For example:

int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

This creates a two-dimensional array called “matrix” with 3 rows and 3 columns. To access an element of the array, you can use the indexing operator ([]), followed by the row and column indices:

int element = matrix[1][2]; // element = 6

You can also use a loop to iterate over the elements of the array. Here is an example using a nested for loop:

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

2. Jagged arrays

These are arrays that have a different number of elements in each row. For example:

int[][] jagged = {{1, 2, 3}, {4, 5}, {6, 7, 8, 9}};

This creates a jagged array called “jagged” with 3 rows and a variable number of columns. To access an element of the array, you can use the indexing operator ([]), followed by the row and column indices:

int element = jagged[1][1]; // element = 5

You can also use a loop to iterate over the elements of the array. Here is an example using a nested for loop:

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

How to Declare Multidimensional Array in Java?

In Java, you can create a multidimensional array by using the “new” operator and specifying the size of each dimension.

Here is an example of how to create a two-dimensional array in Java:

int[][] matrix = new int[3][3];

This creates a two-dimensional array called “matrix” with 3 rows and 3 columns. The array is automatically initialized with the default value for the element type (0 for int).

You can also create and initialize a two-dimensional array in a single step, like this:

int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

Here is an example of how to create a jagged array in Java:

int[][] jagged = new int[3][];
jagged[0] = new int[3];
jagged[1] = new int[2];
jagged[2] = new int[4];

This creates a jagged array called “jagged” with 3 rows and a variable number of columns. The array is not initialized with any values, so you will need to assign values to the elements manually.

Example using 2d Array in Java

Here is an example of how to use a two-dimensional array in Java to store and manipulate a list of integers:

public class TwoDimensionalArrayExample {
  public static void main(String[] args) {
    // Declare and create a two-dimensional array
    int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

    // Access and modify an element of the array
    matrix[0][0] = 100;
    System.out.println(matrix[0][0]);

    // Find the number of rows and columns in the array
    int rows = matrix.length;
    int columns = matrix[0].length;
    System.out.println("Rows: " + rows);
    System.out.println("Columns: " + columns);

    // Iterate over the elements of the array
    for (int i = 0; i < matrix.length; i++) {
      for (int j = 0; j < matrix[i].length; j++) {
        System.out.println(matrix[i][j]);
      }
    }
  }
}

This program declares and creates a two-dimensional array called “matrix”, and then it uses several methods to manipulate the array:

  • The indexing operator ([]) is used to access and modify an element of the array.
  • The “length” property is used to find the number of rows and columns in the array.
  • A nested for loop is used to iterate over the elements of the array.

The output of the program is:

100
Rows: 3
Columns: 3
100
2
3
4
5
6
7
8
9

Leave a Reply