+ 1

How can i print an array in java?

I tryed System.out.println(arr); but i only got some weird output. In web i found System.out.println(Array.toString(arr)); but that one doesnt work.

19th Oct 2017, 5:28 AM
Grafit-1
3 odpowiedzi
+ 5
for Arrays.toString() to work, you will need to use it from java.util. (You also forgot the s in Arrays). You can import it like so: import java.util.Arrays; // @top of program System.out.println(Arrays.toString(arr)); Note* this will include the brackets [ ], you can use what the others posted if you don't want the brackets.
25th Oct 2017, 8:02 PM
Rrestoring faith
Rrestoring faith - avatar
+ 4
If you want to print all items in that array, just do it with a for loop: for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); }
19th Oct 2017, 5:43 AM
Cool Codin
Cool Codin - avatar
+ 1
Lot's of ways brother. for(int i : array){ System.out.println(i); }
19th Oct 2017, 6:10 AM
Nick Nderitu
Nick Nderitu - avatar