Example directory The examples below look at a directory…
Java打印数组 Java Print Array Examples
Arrays are usually useful when working with arbitrarily large number of data having the same type. It is usually convenient if we can print the contents of an array. Below are some examples on how to print the contents of an Array in Java.
Print Array In Java Using Default toString()
All classes in Java has the toString() method. If we use the toString() method of an array object for printing, the result is not good. Here is an example:
/** * 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
Print Array In Java Using Own Implementation
To fix the problem, we can always use our own logic/implementation to display each item of an array in Java. Below is an example:
/** * 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, 10
Which 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); } }
Print Array In Java Using Arrays.toString()
The java.util.Arrays class has some nice utility functions for our use case. Below is an example on how to use the class in printing the contents of a Java Array:
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.
Print Array In Java Using Arrays.deepToString()
For multi-dimensional arrays, the method Arrays.deepToString() is more appropriate. It will do a deep String representation of an array, that reflects the dimension using square brackets. Below is a simple example using two 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, 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]]]
原文:http://javadevnotes.com/java-print-array-examples
本文: Java打印数组 Java Print Array Examples