0

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
https://code.sololearn.com/cFZw4c01m60d/?ref=app 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 https://code.sololearn.com/c3yWe9DKbJr1 (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