Question:
What is the simplest way to print the contents of an Array?
Code:
import java.util.Arrays; public class PrintArray { public static void main(String[] args) { int[] ints = new int[] {1, 2, 3, 4, 5}; String[] strings = new String[] {"Tom", "Mary", "Bill", "Hank"}; System.out.println(Arrays.toString(ints)); System.out.println(Arrays.toString(strings)); } }
Output:
$ java PrintArray [1, 2, 3, 4, 5] [Tom, Mary, Bill, Hank]