欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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));
	}
}


Results:


[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.