0

Why this code make this output? ( It ouputs [I@xxxxxxxx ) without any errors

Code: public class Main { public static void main(String[] args) { int[] map = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; System.out.println(map); } } I am using another compiler, not sololearn's playground

11th Aug 2020, 1:55 PM
DragonArial
DragonArial - avatar
6 Réponses
+ 1
You are printing the memory address of the array and it will give you the same result everytime you run it. You can use the Arrays class and call the toString method and pass your array (map) as parameter: System.out.println( Arrays.toString(map) ); You can also use an for-each loop if you want: for(int elemens: map) System.out.println(elemens);
11th Aug 2020, 2:42 PM
JavaBobbo
JavaBobbo - avatar
0
I think it outputs the adress of the map? Well map is defined as int[] means map points to the computer adress of the first 1 Try to print map[0] to accesss first element More to arrays in Java: https://www.sololearn.com/learn/Java/2148/
11th Aug 2020, 1:58 PM
Alexander Thiem
Alexander Thiem - avatar
0
The output is random every time
11th Aug 2020, 1:59 PM
DragonArial
DragonArial - avatar
0
Try this: System.out.println(map.toString); At the last! And I hope you know that you have made a uni dimensional array
11th Aug 2020, 2:08 PM
Namit Jain
Namit Jain - avatar
0
Thanks For all answers!
11th Aug 2020, 2:49 PM
DragonArial
DragonArial - avatar
0
you are welcome
11th Aug 2020, 3:12 PM
Alexander Thiem
Alexander Thiem - avatar