Dump Multi-Dimensional arrays in Java

Java 8Object Oriented ProgrammingProgramming

A multi-dimensional array can be easily printed by using the method java.util.Arrays.deepToString() in Java. This method converts the multi-dimensional array to string and prints the array contents enclosed in square brackets.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.util.Arrays;
public class Demo {
   public static void main(String args[]) {
      int arr [][]= { {5, 8, 6, 3}, {1, 9, 7}, {4, 8, 2}, {6} };
      System.out.println("The multi-dimensional array content is:");
      System.out.println(Arrays.deepToString(arr));
   }
}

Output

The multi-dimensional array content is:
[[5, 8, 6, 3], [1, 9, 7], [4, 8, 2], [6]]

Now let us understand the above program.

The multi-dimensional array arr[][] is defined. Then the array content is printed using the Arrays.DeepToString() method. A code snippet which demonstrates this is as follows −

int arr [][]= { {5, 8, 6, 3}, {1, 9, 7}, {4, 8, 2}, {6} };
System.out.println("The multi-dimentional array content is:");
System.out.println(Arrays.deepToString(arr));
raja
Published on 09-Jan-2019 11:13:59
Advertisements