开发者

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

开发者 https://www.devze.com 2023-02-24 01:44 出处:网络
This question already has answers here: Why does my ArrayList contain N copies of the last item added to the list?
This question already has answers here: Why does my ArrayList contain N copies of the last item added to the list? (5 answers) Closed 6 years ago.

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.

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 cre开发者_C百科ated 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

As Requested - XmlHandler.java - http://pastebin.com/mGmWt1RD ParsedDataSet.java = http://pastebin.com/k1xb3KBe Content.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.

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.

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 just stores reference of elements. Ensure that your code looks like:

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

精彩评论

暂无评论...
验证码 换一张
取 消