Home > Net >  Create array in the Map in Java
Create array in the Map in Java

Time:02-10

I want to create a array in the map value, I have some code as below:

Map<String, int[]> map = new TreeMap<String, int[]>();

int[] movies = new int[2];
movies[0] = 2;
movies[1] = 3;
map.put("1", movies);
System.out.println(Arrays.toString(map.get("1")));

movies[0] = 1;
movies[1] = 4;
map.put("2", movies);

System.out.println(Arrays.toString(map.get("1")));

The first output is [2,3]
The second output is [1,4]

I expected the second output to be [2,3] as well. If I created new array and put new array into the second put, then it works. Does anyone know why the second output is not [2,3]?

Thanks

CodePudding user response:

  •  Tags:  
  • Related