ITeye博客

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

prevention · 2012-10-14 01:45 · Markdown

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.

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。