Map<Integer,List<Integer>> bossMap = new HashMap<Integer,List<Integer>>();
List<Integer> arrayBoss = new ArrayList<Integer>();
for(int i=0;i<10;i++){
bossMap.put(i,arrayBoss);
}
System.out.println(bossMap);
int curX = 0;
int curY = 1;
for(int i=0;i<10;i++){
List<Integer> arrayBoss1 = new ArrayList<Integer>();
curX = scanner.nextInt();
curY = scanner.nextInt();
System.out.println(curX);
System.out.println(curY);
if(bossMap.containsKey(curX))
arrayBoss1= bossMap.get(curX);
arrayBoss1.add(curY);
bossMap.put(curX,arrayBoss1);
System.out.println(arrayBoss1);
}
HashMap不能初始化ArrayList的值,会导致指向同一个引用arrayBoss ,最终导致map中key1到10的value都是相同的,那这个时候就不能初始化了,直接去除第一个for循环。