How to Print an Array in Java

How to Print an Array in Java | How to Print a 2D array in Java | In this post, we will see the different ways to print an array in Java.

Print an Array using loops

Assume we have a one-dimensional array of numbers than to display it, iterator through the array using its index, get the element at that index, and then display it.

Java program to display array using for loop

public class DisplayArray {
  public static void main(String[] args) {
    
    // declare and initialize an array
    int arr[] = {10, 20, 30, 40, 50};
    
    // display the array using for loop
    for(int i=0; i<arr.length; i++) {
      System.out.print(arr[i] + " ");
    }
  }

}

Output:-

10 20 30 40 50

The “length” is an in-build property of an array which is helpful to find the length of an array. For example- arr.length gives the length of array arr or the number of elements in the array arr.

Java program to display array using for-each loop

public class DisplayArray {
  public static void main(String[] args) {
    
    // declare and initialize an array
    int arr[] = {10, 20, 30, 40, 50};
    
    // display the array using for-each loop
    for(int i : arr) {
      System.out.print(i + " ");
    }
  }

}

Output:-

10 20 30 40 50 

The for-each loop was introduced in Java5, and it is another array traversing technique like for loop, while loop, do-while loop.

for(int i=0; i<arr.length; i++) {
System.out.print(arr[i] + " ");
}

is similar to

for(int i : arr) {
System.out.print(i + " ");
}

In the previous example, we had taken hardcoded values i.e. the array elements were given inside the program. Now, let us develop another similar program by taking input values from the end-user.

import java.util.Scanner;

public class DisplayArray {
  public static void main(String[] args) {
    
    // create Scanner class object to read input
    Scanner scan = new Scanner(System.in);
    
    // declare an array variable of int type
    int arr[] = null;
    
    // ask the length
    System.out.print("Enter array length: ");
    int length = scan.nextInt();
    
    // assign size to the array
    arr = new int[length];
    
    // take input values
    System.out.println("Enter array elements: ");
    for(int i=0; i<arr.length; i++) {
      arr[i] = scan.nextInt();
    }
    
    // display the array using for loop
    System.out.println("The Entered array: ");
    for(int i=0; i<arr.length; i++) {
      System.out.print(arr[i] + " ");
    }
  }

}

Output:-

Enter array length: 5
Enter array elements:
15 25 35 45 55
The Entered array:
15 25 35 45 55

Print an Array in Java using Arrays.toString()

In Java, Arrays is a pre-defined class given in java.util package which contains lots of pre-defined methods related to the array, and they solves many common array task. In Arrays class toString() method is given to display the elements in the given array. Therefore, whenever you need to display a one-dimensional array of primitive data types then it is recommended to use Arrays.toString() method. The Arrays.toString() method places array elements inside the bracket [] while displaying them.

Java program to print an array using Arrays.toString() method

// import Arrays class
import java.util.Arrays;

public class DisplayArray {
  public static void main(String[] args) {
    
    // declare and initialize an array
    int arr[] = {10, 20, 30, 40, 50};
    
    // display the array using Arrays.toString()
    System.out.print(Arrays.toString(arr));
  }

}

Output:-

[10, 20, 30, 40, 50]

Print a 2D array in Java using loops 

To display a multi-dimensional array we need N nested loops for an N-dimensional array. For example to display a two-dimensional array (2d) array we need two loops, for a three-dimensional array we need to take three loops. The loop can be either for loop, for each loop, while loop, do-while loop.

Print 2D array in Java using for-each loops

public class Display2DArray {
  public static void main(String[] args) {
    
    // declare and initialize an array
    int arr[][] = {{50,60},{70,80},{90,100}};
    
    // display 2D array using for-each loop
    for(int[] i : arr) {
      for(int j : i) {
        System.out.print(j + " ");
      }
    }
  }
}

Output:-

50 60 70 80 90 100 

Display 2D array in Java using for loops

public class Display2DArray {
  public static void main(String[] args) {
    
    // declare and initialize an array
    int arr[][] = {{50,60},{70,80},{90,100}};
    
    // display 2D array using for loop
    for(int i=0; i<arr.length; i++) {
      for(int j=0; j<arr[i].length; j++) {
        System.out.print(arr[i][j] + " ");
      }
    }
  }
}

Output:-

50 60 70 80 90 100 

Display 2D Array in using Arrays.deepToString()

Similar to the toString() method the Arrays class also contains deepToString(), which can be used to display multidimensional array. Note:- The Arrays.deepToString() method can’t be used to display the one-dimensional array).

import java.util.Arrays;

public class Display2DArray {
  public static void main(String[] args) {
    
    // declare and initialize an array
    int arr[][] = {{50,60},{70,80},{90,100}};
    
    // display 2D array using Arrays.deepToString()
    System.out.print(Arrays.deepToString(arr));
  }
}

Output:-

[[50, 60], [70, 80], [90, 100]]

Display 3D Array

Using Arrays.deepToString() method we can display any multidimensional array. Here is an example of the Java program to display a three-dimensional array in Java.

import java.util.Arrays;

public class Display3DArray {
  public static void main(String[] args) {
    
    // declare and initialize an array
    int arr[][][] = { {{1,2},{3,4},{5,6}}, {{7,8},{9,1},{2,3}} };
    
    // display 3D array using  Arrays.deepToString()
    System.out.print(Arrays.deepToString(arr));
  }
}

Output:-

[[[1, 2], [3, 4], [5, 6]], [[7, 8], [9, 1], [2, 3]]]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply