Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在循环中重新初始化HashMap值_Java_Collections_Arraylist_Hashmap - Fatal编程技术网

Java 在循环中重新初始化HashMap值

Java 在循环中重新初始化HashMap值,java,collections,arraylist,hashmap,Java,Collections,Arraylist,Hashmap,这是我的密码 public class Test { public static void main(String[] args) { ArrayList<Integer> temp = new ArrayList<Integer>(); HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, Arr

这是我的密码

public class Test {
    public static void main(String[] args) {                
        ArrayList<Integer> temp = new ArrayList<Integer>();
        HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();
        int serialno = 0;

        for (int i = 1000; i < 1004; i++) {
            temp.clear();

            temp.add(i);
            temp.add((i+1000));

            map.put(serialno++, temp);
        }
        System.out.println(map);
    }
}
但我得到的结果是

{0=[1003, 2003], 1=[1003, 2003], 2=[1003, 2003], 3=[1003, 2003]}

这里发生了什么事。?我哪里出错了?

您只创建了一个ArrayList对象,并将其多次放入HashMap中,这样您的HashMap中就有许多对同一对象的引用,但您需要多个ArrayList对象,因此必须在循环中创建它们

public class Test {
    public static void main(String[] args) {
        HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();
        int serialno = 0;

        for (int i = 1000; i < 1004; i++) {
            ArrayList<Integer> temp = new ArrayList<Integer>();

            temp.add(i);
            temp.add((i+1000));

            map.put(serialno++, temp);
        }

        System.out.println(map);
    }
}

清除ArrayList临时值时,只需删除其值。但是,将ArrayList放入映射时,实际上是将ArrayList的对象引用放入映射,而不是包含新值的新列表。所以在你的地图中是相同ArrayList的四倍,因此是相同值的四倍

 HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();
 int serialno = 0;

 for (int i = 1000; i < 1004; i++) {
      ArrayList<Integer> temp = new ArrayList<Integer>();
      temp.add(i);
      temp.add((i + 1000));
      System.out.println(i + 1000);

      map.put(serialno++, temp);
 }
 System.out.println(map);

在每个循环中创建新列表时,实际上会在映射中放置具有不同引用的不同ArrayList。

您正在重用同一hashmap,因此任何serialno的值显然都是相同的。当您将对象放入映射或列表中时,对象不会被复制。相反,指向对象的引用指针被放入地图中。temp=new ArrayList和temp.cleartemp.clear的区别是什么?从同一个对象中删除项目以便获得相同的结果,您需要在每次旋转时创建一个新对象
 HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();
 int serialno = 0;

 for (int i = 1000; i < 1004; i++) {
      ArrayList<Integer> temp = new ArrayList<Integer>();
      temp.add(i);
      temp.add((i + 1000));
      System.out.println(i + 1000);

      map.put(serialno++, temp);
 }
 System.out.println(map);