Print out an array | Sololearn: Learn to code for FREE!

Print out an array

How can i print out the whole array? if i do int [] arr = {2, 3, 4}; System.out.println(arr); it outputs strange characters. i suppose i can do it with for loop, but than i get the numbers one by one, not the whole array.

14th Apr 2017, 6:11 AM
Benedek Máté Tóth
Benedek Máté Tóth - avatar
4 Answers
+ 6 Arrays.toString(arr) for one-dimensional array and Arrays.deepToString(arr) for multi-dimensional.
14th Apr 2017, 12:37 PM
Tamás Barta
Tamás Barta - avatar
+ 2
Your are right about the loop. In Java, arrays have a length property which you can use in a for loop to walk trough the array, one by one. If you don't care about the (loss of) the data type and just want the array at a whole printed to the screen, you can import Arrays (import java.util.Arrays) and use the toString method, see (I haven't set the code example to "Public" and I'm not certain whether it can be linked anyway. If this doesn't work, please just drop me a line.)
14th Apr 2017, 6:28 AM
Klaus-Dieter Warzecha
Klaus-Dieter Warzecha - avatar
+ 2
for(int a=0;a<arr.length;a++){ System.out.println(arr[a]); } this is it.
14th Apr 2017, 7:38 AM
Yasiru Nayanajith
Yasiru Nayanajith - avatar
+ 1
thanks for help to everybody, i got it, links worked fine!
14th Apr 2017, 9:38 PM
Benedek Máté Tóth
Benedek Máté Tóth - avatar