Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.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 mp=新的HashMap(); List myList=new ArrayList(); 整数x=0; 整数y=5; 无效测试(){ 对于(整数i=0;i 新建for循环中的一个。这样,映射将为每个整数保存一个唯一的列表 public class ListMap { HashMap<Integer, List> mp = new HashMap(); List myList = new ArrayList(); Integer x = 0; Integer y = 5; void test() { for(Integer i = 0; i < 5; i++) { x = y; myList.add("check-1a" + i); myList.add("check-1a" + i + 1); y = null; System.out.println(x); // output=5 mp.put(i, myList); myList.clear(); } } 公共类列表映射{ HashMap mp=新的HashMap(); //List myList=new ArrayList();//****摆脱 整数x=0; 整数y=5; 无效测试(){ 对于(整数i=0;i_Java - Fatal编程技术网

Java集合哈希映射,值意外更改 公共类列表映射{ HashMap mp=新的HashMap(); List myList=new ArrayList(); 整数x=0; 整数y=5; 无效测试(){ 对于(整数i=0;i 新建for循环中的一个。这样,映射将为每个整数保存一个唯一的列表 public class ListMap { HashMap<Integer, List> mp = new HashMap(); List myList = new ArrayList(); Integer x = 0; Integer y = 5; void test() { for(Integer i = 0; i < 5; i++) { x = y; myList.add("check-1a" + i); myList.add("check-1a" + i + 1); y = null; System.out.println(x); // output=5 mp.put(i, myList); myList.clear(); } } 公共类列表映射{ HashMap mp=新的HashMap(); //List myList=new ArrayList();//****摆脱 整数x=0; 整数y=5; 无效测试(){ 对于(整数i=0;i

Java集合哈希映射,值意外更改 公共类列表映射{ HashMap mp=新的HashMap(); List myList=new ArrayList(); 整数x=0; 整数y=5; 无效测试(){ 对于(整数i=0;i 新建for循环中的一个。这样,映射将为每个整数保存一个唯一的列表 public class ListMap { HashMap<Integer, List> mp = new HashMap(); List myList = new ArrayList(); Integer x = 0; Integer y = 5; void test() { for(Integer i = 0; i < 5; i++) { x = y; myList.add("check-1a" + i); myList.add("check-1a" + i + 1); y = null; System.out.println(x); // output=5 mp.put(i, myList); myList.clear(); } } 公共类列表映射{ HashMap mp=新的HashMap(); //List myList=new ArrayList();//****摆脱 整数x=0; 整数y=5; 无效测试(){ 对于(整数i=0;i,java,Java,1) 但是在用myList.clear()清除列表后,映射中的值也会被清除 我的意思是,map键仍然存在,但它包含一个“空”列表 2) 然而,对于对象x&y,在将y设置为null后,x为什么没有改变?您将同一列表多次添加到地图中并每次清除它,因此它为空也就不足为奇了。解决方案:不清除列表,创建一个g> 新建for循环中的一个。这样,映射将为每个整数保存一个唯一的列表 public class ListMap { HashMap<Integer, List> mp = new

1) 但是在用
myList.clear()清除列表后,映射中的值也会被清除

我的意思是,map
键仍然存在,但它包含一个“空”列表


2) 然而,对于对象
x
&
y
,在将
y
设置为
null
后,
x
为什么没有改变?

您将同一列表多次添加到地图中并每次清除它,因此它为空也就不足为奇了。解决方案:不清除列表,创建一个g> 新建for循环中的一个。这样,映射将为每个整数保存一个唯一的列表

public class ListMap {
    HashMap<Integer, List> mp = new HashMap();
    List myList = new ArrayList();
    Integer x = 0;
    Integer y = 5;

    void test() {
    for(Integer i = 0; i < 5; i++) {
        x = y;
        myList.add("check-1a" + i);
        myList.add("check-1a" + i + 1);
        y = null;
        System.out.println(x); // output=5
        mp.put(i, myList);
        myList.clear();
    }
}
公共类列表映射{
HashMap mp=新的HashMap();
//List myList=new ArrayList();//****摆脱
整数x=0;
整数y=5;
无效测试(){

对于(整数i=0;i当您将对象添加到映射(或任何其他集合)时,您是在添加对该对象的引用,而不是副本。当您随后对该对象进行更改时,这些更改也将影响映射中的引用

当您想要存储列表的副本时,需要创建一个新的

public class ListMap {
     HashMap<Integer,List<String>> mp=new HashMap<>();
     // List<String> myList=new ArrayList<String>(); // **** get rid of
    Integer x=0;
    Integer y=5;
    void test(){
        for(Integer i=0;i<5;i++){
            List<String> myList=new ArrayList<String>();  // ****** here 
            x=y;
            myList.add("check-1a"+i);
            myList.add("check-1a"+i+1);
            y=null;
            System.out.println(x);//output=5
            mp.put(i,myList);
            // myList.clear();  // **** get rid of
        }
   }
另一种解决方案(我认为更好)是在每次循环迭代开始时通过将myList设置为一个新的list对象来重新初始化它:

mp.put(i, new ArrayList(myList));
请注意,当您重新初始化变量
myList
时,列表不会被破坏。您可以想象对象生活在地图*内


*虽然从技术上来说,更准确的描述应该是“该对象存在于内存中,只要映射中仍然存在对它的引用,它就不会被垃圾收集”

我处理了它,但我无法理解为什么在清除列表之前,我将列表的值放在地图中,然后将地图中的值清除为well@ShashiRanjan:您处理了什么??您只创建了一个列表,并多次添加同一个已清除列表。不要这样做。关键概念是只有一个A所有原始代码中都有一个rrayList对象。类似地,我使用了两个对象X&Y,并在Y对象中放入NULL,那么为什么X包含以前的Y值???@HovercraftFullOfEels
不要这样做。
…既然您提到了……那么您对原始类型的使用情况如何(
HashMap
List
/
ArrayList
)? :P@ShashiRanjan代码中的
x
y
只是保存对象引用的变量,而不是对象。当您编写
x=y
时,您将
x
指向与
y
相同的
整数
对象。然后,当您将
y
设置为
null
时,您将该变量设置为ints不指向任何对象。
x
仍然包含对以前被
y
引用的对象的引用。不要使用原始类型,如
List
ArrayList
。您应该在示例中修复正确的缩进。@David Conrad请详细说明使用什么来代替它???您应该使用参数化类型,如(在本例中)
HashMap
List
ArrayList
。thanx我也会这样做。。。
myList = new ArrayList();
myList.add("check-1a"+i);
myList.add("check-1a"+i+1);
mp.put(i,myList);