javacodex.com
Java Examples
Java Examples
memu home questions

Simple Way To Print The Contents Of An Array

This is a simple java example on how to print the contents of an array.

Source: (PrintArray.java)

import java.util.*;
 
public class PrintArray {
   public static void main(String[] args) {
 
      int[] a1 = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
      System.out.println(Arrays.toString(a1));
 
      String[] a2 = new String[] {"Paul", "John", "Ringo", "George"};
      String[] a3 = new String[] {"Bob", "Tom", "Mary", "Tony"};
      String[][] a4 = { a2, a3 };
 
      //Use Arrays.deepToString() if array contains other arrays as elements
      System.out.println(Arrays.deepToString(a4));
   }
}

Output:

$ java PrintArray 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[[Paul, John, Ringo, George], [Bob, Tom, Mary, Tony]]

Contact: javacodex@yahoo.com