为什么变异一个ArrayList中的对象在所有其他ArrayList中改变它?(Why does mutating an object in one ArrayList change it in al

编程入门 行业动态 更新时间:2024-10-23 18:22:37
为什么变异一个ArrayList中的对象在所有其他ArrayList中改变它?(Why does mutating an object in one ArrayList change it in all other ArrayLists?)

我正在制作一个cpu调度模拟器(用于学校项目)。 我的roundRobin函数有问题。 当我做c.get(i).jobTime -= 2; 和c.get(i).jobTime -= 1; 它影响我的其他ArrayList ,所以我不能做我的其他功能。 在我打电话给roundRobin2之前,我的名单完全正常。 为什么会发生?

例如,这是我的list4在roundRobin2之后的roundRobin2

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

这是我如何读取文件,并将Jobs对象放入我的ArrayList 。

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)); }

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?

For example this is how my list4 looks like after roundRobin2

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

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();

This is my 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个列表中。 当你改变这个对象时,你可以改变它。 该突变将反映在所有列表中,因为它们都存储相同的对象引用。

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

相反,您需要为每个列表添加一个新的对象引用(如果需要将对象的克隆存储在每个列表中)。

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的任何两个列表,都为true )。 在第二个示例中,它将是false因为您现在有完全不相关的对象,它们恰好在它们添加到列表中时存储相同的值。

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)); }

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.

更多推荐

本文发布于:2023-08-06 15:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1450231.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   mutating   ArrayList   ArrayLists   change

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!