/** * A Simple Program That Prints An Array In Java. */ public class PrintArrayInJava { private static void printArray(int[] anArray) { System.out.println(anArray.toString()); } public static void main(String[] args) { int[] testArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; printArray(testArray); } }
The output is shown below. This is not very useful because we usually want the contents to be seen in the display.
[I@70f9f9d8
/** * A Simple Program That Prints An Array In Java using Own logic. */ public class PrintArrayInJava { private static void printArray(int[] anArray) { for (int i = 0; i < anArray.length; i++) { if (i > 0) { System.out.print(", "); } System.out.print(anArray[i]); } } public static void main(String[] args) { int[] testArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; printArray(testArray); } }The output will be:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10Which reflects the contents of the array. Below is another implementation using StringBuilder instead of displaying on screen one item at a time:
/** * A Simple Program That Prints An Array In Java using Own logic. */ public class PrintArrayInJava { private static void printArray(int[] anArray) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < anArray.length; i++) { if (i > 0) { sb.append(", "); } sb.append(anArray[i]); } System.out.println(sb.toString()); } public static void main(String[] args) { int[] testArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; printArray(testArray); } }
import java.util.Arrays; /** * A Simple Program That Prints An Array In Java using Arrays.toString(). */ public class PrintArrayInJava { private static void printArray(int[] anArray) { System.out.println(Arrays.toString(anArray)); } public static void main(String[] args) { int[] testArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; printArray(testArray); } }
First, we need to import the Arrays class to our program:
import java.util.Arrays;
We can then invoke:
Arrays.toString(anArray)
To convert the array to it's String representation. The resulting String is passed to the println method to display on the screen. The output is below. Notice the square brackets on the output, in addition to the elements separated with commas.
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The Arrays.toString() method can't be used with a arrays with dimension two or greater. Here is a simple illustration, this code:
import java.util.Arrays; /** * A Simple Program That Prints An Array In Java using Arrays.toString(). */ public class PrintArrayInJava { private static void printArray(int[][] anArray) { System.out.println(Arrays.toString(anArray)); } public static void main(String[] args) { int[][] testArray = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; printArray(testArray); } }
Will output this result on screen:
[[I@70f9f9d8, [I@2b820dda, [I@675b7986]Because the method will not do a deep conversion. It will only iterate on the first dimension and call the toString() method of each items. Resulting in unpleasant result.
import java.util.Arrays; /** * A Simple Program That Prints An Array In Java using Arrays.deepToString(). */ public class PrintArrayInJava { private static void printArray(int[][] anArray) { System.out.println(Arrays.deepToString(anArray)); } public static void main(String[] args) { int[][] testArray = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; printArray(testArray); } }
The output is below. Notice how the square brackets denotes the level of dimension.
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Here is another example using three dimensional array:
import java.util.Arrays; /** * A Simple Program That Prints An Array In Java using Arrays.deepToString(). */ public class PrintArrayInJava { private static void printArray(int[][][] anArray) { System.out.println(Arrays.deepToString(anArray)); } public static void main(String[] args) { int[][][] testArray = { { { 1, 2 }, { 4, 5 } }, { { 5, 6 }, { 7, 8 } } }; printArray(testArray); } }
And this is the corresponding output. The square brackets are also 3 levels deep.
[[[1, 2], [4, 5]], [[5, 6], [7, 8]]]