Thinking in Java 2: Arrays.toString vs. Arrays.deepToString
程序员文章站
2024-03-06 22:04:44
...
In Thinking in Java 1: Returning an array, I use a low-efficient method to print out an array. Now I wanna introduce you an easy way to show an array, even a multidimensional array.
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[][] a = {{1,2,3}, {4,5,6}};
String[] b = {"abc", "def"};
System.out.println(Arrays.deepToString(b));
System.out.println(Arrays.deepToString(a));
}
}
[abc, def]
[[1, 2, 3], [4, 5, 6]]
This example uses the Java SE5 Arrays.deepToString() method, which turns multidimentional arrays into Strings, as you can see from the output.