A two-dimensional array is an array within an array. In this type of array, the position of an element is referred to by two indices instead of one.

A two-dimensional array can be printed using two for loops which iterate through each index of the array.

Let us consider a 2D array arr[3][3] = { {10, 20, 30} , {40, 50, 60} , {70, 80, 90}}. The example below illustrates how to print the array using for loop.

import java.util.*;
public class twodarry {
  public static void main(String args[]) {
    int rows = 3, cols = 3;
    int arr[][] = { {10, 20, 30}, {40, 50, 60}, {70, 80, 90} };
    for (int i = 0; i < rows; i++) {
      for (int j = 0; j < cols; j++) {
        System.out.print("\t" + arr[i][j]);
      }
    }
  }
}

The output is

10	20	30	40	50	60	70	80	90

for each loop is mainly used to traverse the array or collection. It is known as the for-each loop because it traverses each element in an array one by one.

The main difference between for loop and for each loop is that in for loop the number of iteration has to be set manually, whereas in for each loop the iteration is counted automatically depending on the length of an array.

Let us take an example of a 2D array demo [2][3] = { {04, 09, 25}, {36, 49, 64}}. Here we use 2 for each loops, the first loop iterates through the rows and the second loop iterates through columns.

import java.io.*;
public class foreachex {
  public static void main(String args[]) {
    int demo[][] = {{4, 9, 25}, {36, 49, 64}};
    for (int x[]: demo) {
      for (int y: x) {
        System.out.print(y + " ");
      }
    }
  }
}

The output is

4 9 25 36 49 64 

Using Arrays.toString() method

The java.util.Arrays.toString() method returns a string representation of the contents of the specified array. The string representation consists of a list of the array's elements, enclosed in square brackets ("[ ]"), adjacent elements are separated by a ", " (comma).

Let us consider an example array arry[4][3] = {{80, 100, 120}, {140, 160, 180}, {200, 220, 240}, {260, 280, 300}}. The Arrays.toString() method gives the string representation of the array arr in a matrix form.

import java.io.*;
import java.util.*;
public class arrystostr {
  public static void matrix(int a[][]) {
    for (int[] row: a)
      System.out.println(Arrays.toString(row));
  }
  public static void main(String args[]) {
    int arry[][] = {{80, 100, 120}, {140, 160, 180}, {200, 220, 240}, {260, 280, 300}};
    matrix(arry);
  }
}

The output is

[80, 100, 120]
[140, 160, 180]
[200, 220, 240]
[260, 280, 300]
Using Arrays.deepToString() method

The java.util.Arrays.deepToString() method returns a string representation of the deep contents of the specified array. The simple toString() method works well for simple arrays but doesn’t work for multidimensional arrays. This method is designed for converting multidimensional arrays to strings.

Let us consider an example arr[4][2] = {{100, 200}, {300, 400}, {500, 600}, {700, 800}}. The Arrays.deepToString() method represents the string representation of the array arr.

import java.io.*;
import java.util.*;
public class deepstostr {
  public static void main(String args[]) {
    int arr[][] = {{100, 200}, {300, 400}, {500, 600}, {700, 800}};
    System.out.println(Arrays.deepToString(arr));
  }
}

The output is

[[100, 200], [300, 400], [500, 600], [700, 800]]
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions