`
prevention
  • 浏览: 71110 次
社区版块
存档分类
最新评论

Thinking in Java 2: Arrays.toString vs. Arrays.deepToString

 
阅读更多

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.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics