How to create Multidimensional array program in Java

class Multi_Array 
{
    public static void main(String args[]) 
    {
        // Declaring and initializing 2D array
        int arr[][] = { {1, 2, 3}, {2, 4, 5}, {4, 4, 5} };

        // Printing 2D array
        for (int i = 0; i < 3; i++) 
        {
            for (int j = 0; j < 3; j++) 
            {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Step-by-Step Breakdown:

1. Declare and Initialize the 2D Array:

int arr[][] = {{1, 2, 3}, {2, 4, 5}, {4, 4, 5}};

Here, arr is a 2D array (an array of arrays) of type int.

  • It has been declared with 3 rows and 3 columns, hence it contains 9 elements in total.
  • The first row contains {1, 2, 3}, the second row contains {2, 4, 5} and the third row contains {4, 4, 5}.

Here’s the way array looks:

arr = {

   {1, 2, 3},

   {2, 4, 5},

   {4, 4, 5}

}

2. Outer Loop for Rows:

for (int i = 0; i < 3; i++) {  // Loop through the rows

  • The outer loop will go round every row of the 2D array.
  • i begins at 0 and takes on values up to 2 because there are 3 rows in the array.
  • The loop variable i is the row index.

3. Inner Loop for Columns:

for (int j = 0; j < 3; j++) {  // Loop through the columns

  • The inner loop traverses through the elements in each row that is, columns.
  • j starting from 0 goes up to 2 (as there are 3 columns in each row).
  • The loop variable j holds the column index.

4. Printing Elements of the Array:

System.out.print(arr[i][j] + ” “);

  • arr[i][j] accesses the element at the i-th row and j-th column of the array.
  • System.out.print prints the element along with a space (” “), therefore, the elements of each row will appear on the same line.

5. Print a New Line After Each Row:

System.out.println();

____________________

After printing all the elements of a row, System.out.println() is invoked to print a new line. This will print each row of the 2D array in a new line.

Output:

Here’s how the program runs:

For the first row (arr[0] = {1, 2, 3}, the inner loop will print 1 2 3.

For the second row (arr[1] = {2, 4, 5}, the inner loop will print 2 4 5.

For the third row (arr[2] = {4, 4, 5}, the inner loop will print 4 4 5.

The output will be:

1 2 3

2 4 5

4 4 5

Main Ideas

2D Arrays in Java :

  • A 2D array is essentially an array of arrays. In this case, arr is an array with 3 elements, each of which is an array itself (with 3 elements).
  • It can be visualized as a matrix, where you can access elements using two indices: arr[row][column].

Nested Loops:

  • The outer for loop iterates through the rows of the 2D array.
  • The inner for loop iterates through the columns of the current row.

Array Indexing:

Arrays in Java are 0-indexed, meaning the first element is at index 0, the second at index 1, and so on.

System.out.print() and System.out.println():

System.out.print() prints elements on the same line, while System.out.println() moves to the next line after printing the element.

Conclusion:

This program illustrates the way 2D arrays work in Java: declaring, initializing, and accessing elements of a 2D array. This program will utilize nested for loops to iterate and print out elements of an array in a matrix-like fashion.

Leave a Comment

Your email address will not be published. Required fields are marked *