当一个新加入的ArrayList变化的所有元素? [英] All elements of An ArrayList change when a new one is added?

查看:160
本文介绍了当一个新加入的ArrayList变化的所有元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,道歉不能够提供任何来源$ C ​​$ C。我的项目是pretty的大和链接都将是不切实际的,而且我一直没能缩小是非常恼人的问题。我会尽我所能在这里解释一下。

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.

我动态在我的code创建一个类在每次循环的新实例。这个实例是动态给出几个属性,而在循环中,名称,例如。在每个循环的结束,新生成的实例添加到一个又一个,第三类举行一个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.

但问题是,当一个新的元素加入,无论出于何种原因,都previous元素变为完全匹配最新的。我的猜测是,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.

我将不胜感激任何意见和道歉又对这种解释的质量。我将张贴任何特定的一块$ C $的c您不妨去看看

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/mGmWt1RD ParsedDataSet.java = http://pastebin.com/k1xb3KBe Content.java = http://pastebin.com/UxiL2f​​9q

As Requested - XmlHandler.java - http://pastebin.com/mGmWt1RD ParsedDataSet.java = http://pastebin.com/k1xb3KBe Content.java = http://pastebin.com/UxiL2f9q

只是为了减少您的COM prehension时间 - 该项目是一个EPUB阅读器。该XMLHandler是被称为从另一个类SAX解析器不会显示。所述XMLHandler使用3个不同的时间3不同的XML集,以便有一定的杂波那里。

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,持有内容实例将在后面参考(未示出)。我试图将数据传递内容的每个新实例,然后传递到静态的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

推荐答案

我见过的人报告此类问题很多次,它总是归结到这一点:你是真正的没有的创建一个新的实例,但使用代替同一个用于循环的每次迭代。这是一个容易犯的错误,特别是如果你从不同的复制语义语言来了。有许多不同的方式,你可以犯这样的错误;如果您编辑的问题,以显示回路code,我相信我能解释发生了什么。

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.

OK,现在你已经添加了code:的问题是,在内容,所有的数据成员被标记为静态。在Java中,这意味着有由所有的对象共享一个变量 - 即变量具有用于每个对象相同的值。所以实际上你的的创建多个内容对象将ArrayList中,但是他们看起来都一模一样!删除那些静,从内容的数据成员属性,您将所有设置。

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变化的所有元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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