Friday 29 April 2016

java - How can I print an array easily?




I am currently working with arrays, and everytime I need to print one I do a for loop.



System.out.print("[");
for(int i = 0; i < arr.length; i++){

System.out.print(arr[i] + ", ");
}
System.out.println("]");


This seems like a feature that would be built into java (I am using java). Is there a built in way to print arrays?


Answer



You could use:
Arrays.toString(arr)
for normal arrays and/or

Arrays.deepToString(arr)
for arrays within arrays.
Both these methods return the string representation of the array.



See the Arrays docs for more.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...