Java Reference
In-Depth Information
Wouldn't it be nice to avoid writing that loop ever again? Arrays.
toString() provides just what you need. It accepts an array as a parameter and
returns a formatted string representation of the contents of the array. There are
overloaded versions for arrays of all the primitive types and a version for object
types. For the object types, each element's own toString() is used. Therefore
for String[] types, you get the contents of the string. Here is a simple example:
public class Demo {
public static void main (String[] args) {
System.out.println (Arrays.toString (args));
}
} // class Demo
If you run this code with the following command-line parameters:
java Demo Arrays.toString is really cool
you are greeted with
[Arrays.toString, is, really, cool]
10.10.5 Arrays.deepToString() and deepEquals()
There is also a recursive Arrays.deepToString() that works as might be
expected for multidimensional arrays. The following code snippet:
int[][] a = new int[3][4];
a[0][0] = 1;
a[0][1] = 2;
a[0][2] = 3;
a[0][3] = 4;
a[1][0] = 5;
a[1][1] = 6;
a[1][2] = 7;
a[1][3] = 8;
a[2][0] = 9;
a[2][1] = 10;
a[2][2] = 11;
a[2][3] = 12;
System.out.println (Arrays.deepToString (a));
produces
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
Search WWH ::




Custom Search