本文介绍了添加新元素时,ArrayList 的所有元素都会发生变化吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

然而,问题是当添加新元素时,无论出于何种原因,所有先前的元素都会更改以与最新元素完全匹配.我的猜测是 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.

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

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

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

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

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

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.

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

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

推荐答案

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

I've seen folks report this kind of problem many times, and it always comes down to this: you're actually not creating 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.

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

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 are creating 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.

这篇关于添加新元素时,ArrayList 的所有元素都会发生变化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 22:04