为什么在一个 ArrayList 中改变一个对象会在所有其他 ArrayList 中改变它? [英] Why does mutating an object in one ArrayList change it in all other ArrayLists?

查看:26
本文介绍了为什么在一个 ArrayList 中改变一个对象会在所有其他 ArrayList 中改变它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个 CPU 调度模拟器(用于学校项目).我的 roundRobin 函数有问题.当我执行 c.get(i).jobTime -= 2;c.get(i).jobTime -= 1; 它会影响我的其他 ArrayLists,所以我不能做我的其他功能.在我调用 roundRobin2 之前,我的列表完全正常.为什么会发生这种情况?

I am making a cpu scheduling simulator (for a school project). I have a problem with my roundRobin function. When I do c.get(i).jobTime -= 2; and c.get(i).jobTime -= 1; it effects my other ArrayLists, so I can't do my other functions. Before I call roundRobin2 my lists are completely normal. Why does this happen?

例如这是我的 list4roundRobin2

列表4:[Job101 0, Job102 0, Job103 0, Job104 0, Job105 0, Job106 0]

这就是我读入文件的方式,并将Jobs 对象放入我的ArrayLists.

This is how I read in the file, and put the Jobs objects into my ArrayLists.

Scanner input = new Scanner(new File("testdata1.txt"));
ArrayList<Jobs> list = new ArrayList<Jobs>();
ArrayList<Jobs> list2 = new ArrayList<Jobs>();
ArrayList<Jobs> list3 = new ArrayList<Jobs>();
ArrayList<Jobs> list4 = new ArrayList<Jobs>();

Jobs first;

while (input.hasNext()) {
    first = new Jobs(input.next(), input.nextInt());
    list.add(first);
    list2.add(first);
    list3.add(first);
    list4.add(first);
}

input.close();

这是我的roundRobin2

public void roundRobin2(ArrayList<Jobs> c, int sT) {
    int size = c.size();
    int cT = 0;
    int ccT = 0;
    while (!c.isEmpty()) {
        int i = 0;
        System.out.println(c);
        for (i = 0; i < size; i++) {
            if ((c.get(i).jobTime) >= 2) {
                c.get(i).jobTime -= 2;
                cT += 2;

                if ((c.get(i).jobTime) == 0) {
                    ccT += cT;
                }
            } else {
                (c.get(i).jobTime) -= 1;
                cT += 1;

                if ((c.get(i).jobTime) == 0) {
                    ccT += cT;
                }
            }
        }
        for (i = 0; i < size; i++) {
            if ((c.get(i).jobTime) == 0) {
                c.remove(i);
                size = c.size();
            }
        }
    }
    System.out.println("\nAverage completion times: "+ccT+"/"+sT+" = "+((ccT)/sT));
}

推荐答案

在每次迭代中,您只创建一个对象并将其添加到所有 4 个列表中.当你改变那个对象时,你,好吧,改变它.这种变化将反映在所有列表中,因为它们都存储相同的对象引用.

In each iteration you are creating only a single object and adding it to all 4 lists. When you mutate that object you, well, mutate it. That mutation will be reflected in all the lists because they all store the same object reference.

while (input.hasNext()) {
    first = new Jobs(input.next(), input.nextInt());
    list.add(first);
    list2.add(first);
    list3.add(first);
    list4.add(first);
}

相反,您需要为每个列表添加一个新的对象引用(如果您希望将对象的副本存储在每个列表中).

Instead you need to add a new object reference to each list (if you want a clone of the object to be stored in each list).

while (input.hasNext()) {
    String s = input.next();
    int i = input.nextInt();
    list.add(new Jobs(s, i));
    list2.add(new Jobs(s, i));
    list3.add(new Jobs(s, i));
    list4.add(new Jobs(s, i));
}

在第一个代码示例中,list.get(n) == list2.get(n) 将是 true(对于任何有效的 n代码> 以及 4) 中的任意两个列表.在第二个示例中,它将是 false,因为您现在拥有完全不相关的对象,这些对象恰好在添加到列表时存储了相同的值.

In the first code sample list.get(n) == list2.get(n) will be true (true for any valid n and for any two lists from your 4). In the second sample it will be false as you now have completely unrelated objects that just happen to store the same values at the point they are added to the list.

这篇关于为什么在一个 ArrayList 中改变一个对象会在所有其他 ArrayList 中改变它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆