Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I print an array easily? [duplicate]

Tags:

java

arrays

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?

like image 709
ceptno Avatar asked Nov 30 '22 14:11

ceptno


1 Answers

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.

like image 73
David Kroukamp Avatar answered Dec 05 '22 05:12

David Kroukamp