java 添加新元素时,ArrayList 的所有元素都会发生变化吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5608828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:53:16  来源:igfitidea点击:

All elements of An ArrayList change when a new one is added?

javaandroidarraysloopsreference

提问by Mark D

First of all, apologies for not being able to supply any sourcecode. My project is pretty big and linking everything would be impractical, and I have not been able to scale down the problem which is exceedingly annoying. I will do my best to explain it here.

首先,很抱歉无法提供任何源代码。我的项目非常大,链接所有内容是不切实际的,而且我无法缩小这个非常烦人的问题。我会尽我所能在这里解释它。

I am dynamically creating new instances of a class on each loop in my code. This instance is dynamically given a couple of properties while in the loop, 'name' for example. At the end of each loop, the newly generated instance is added to an ArrayList held in a another, 3rd, class.

我在我的代码中的每个循环上动态地创建一个类的新实例。这个实例在循环中被动态地赋予了几个属性,例如'name'。在每个循环结束时,新生成的实例被添加到另一个第三类中的 ArrayList。

The problem however is that when a new element is added, for whatever reason, all previous elements change to match exactly the latest. My guess is that the ArrayList is creating a reference to the dynamically created element so that whenever it changes, they all change, but I do not know how to fix this.

然而,问题是当添加新元素时,无论出于何种原因,所有先前的元素都会更改以与最新的元素完全匹配。我的猜测是 ArrayList 正在创建对动态创建的元素的引用,以便每当它发生变化时,它们都会发生变化,但我不知道如何解决这个问题。

I would be grateful for any advice and apologies again for the quality of this explanation. I will post any specific piece of the code you may wish to see

对于此解释的质量,我将再次感谢您的任何建议和道歉。我将发布您可能希望看到的任何特定代码段

As Requested - XmlHandler.java - http://pastebin.com/mGmWt1RDParsedDataSet.java = http://pastebin.com/k1xb3KBeContent.java = http://pastebin.com/UxiL2f9q

根据要求 - XmlHandler.java - http://pastebin.com/mGmWt1RDParsedDataSet.java = http://pastebin.com/k1xb3KBeContent.java = http://pastebin.com/UxiL2f9q

Just to cut down on your comprehension time - The project is an epub reader. The XMLHandler is being called from a SAX parser in another class not shown. The XMLHandler is used 3 different times for 3 different XML sets so there is some clutter there.

只是为了减少您的理解时间 - 该项目是一个 epub 阅读器。正在从另一个未显示的类中的 SAX 解析器调用 XMLHandler。XMLHandler 用于 3 个不同的 XML 集 3 次不同的时间,因此那里有些混乱。

The problem lies with the 'toc' ArrayList. The 'toc', or TableOfContents, holds the Contents instances to be referenced later (not shown). I am trying to pass data each new instance of 'Content' and then pass that into the static ArrayList

问题在于'toc' ArrayList。'toc' 或 TableOfContents 包含稍后要引用的 Contents 实例(未显示)。我试图将数据传递给“内容”的每个新实例,然后将其传递到静态 ArrayList

回答by Ernest Friedman-Hill

I've seen folks report this kind of problem many times, and it always comes down to this: you're actually notcreating a new instance, but instead using the same one for each iteration of the loop. It's an easy mistake to make, especially if you're coming from a language with different copy semantics. There are a number of different ways you can make this mistake; if you edit your question to show the loop code, I'm sure I'll be able to explain what's happening.

我已经看到人们多次报告这种问题,而且总是归结为:您实际上不是在创建新实例,而是在循环的每次迭代中使用相同的实例。这是一个容易犯的错误,特别是如果您来自具有不同复制语义的语言。您可以通过多种不同的方式犯此错误;如果您编辑问题以显示循环代码,我相信我能够解释发生了什么。

OK, now that you've added the code: the problem is that in "Content", all the data member are marked "static". In Java, that means that there's one variable shared by all objects -- i.e., the variable has the same value for every object. SO in fact you arecreating many Content objects to put in the ArrayList, but they all look identical! Remove those "static" attributes from Content's data members, and you'll be all set.

好的,现在您已经添加了代码:问题是在“内容”中,所有数据成员都被标记为“静态”。在 Java 中,这意味着所有对象共享一个变量——即,该变量对每个对象都具有相同的值。所以实际上您正在创建许多内容对象以放入 ArrayList,但它们看起来都相同!从 Content 的数据成员中删除那些“静态”属性,您就大​​功告成了。

回答by u290629

ArrayListjust stores reference of elements. Ensure that your code looks like:

ArrayList只存储元素的引用。确保您的代码如下所示:

ArrayList list = new ArrayList<>();
loop(...) {
    MyObject newOne = new MyObject();
    newOne.setXXX(xxx);
    list.add(newOne);
}

Wrong code:

错误的代码:

ArrayList list = new ArrayList<>();
MyObject sameOne = new MyObject();
loop(...) {
    sameOne.setXXX(xxx);
    list.add(sameOne);
}