Results 1 to 6 of 6
Thread: ArrayList glitch?
- 05-21-2010, 12:25 AM #1
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
- 05-21-2010, 12:41 AM #2
well, are you creating new instances of the object for each adding to the list.
for example,
this will create a list with three references to the same object instance, which when changing a property in one index, will of course appear to change all indicies in the array.Java Code:MyObject myObj = new MyObject(); List<MyObject> aList = new ArrayList<MyObj>(); aList.add(myObj); aList.add(myObj); aList.add(myObj);
Instead,
would create three different object references into the list.Java Code:List<MyObject> aList = new ArrayList<MyObj>(); aList.add(new MyObject()); aList.add(new MyObject()); aList.add(new MyObject());
- 05-21-2010, 01:05 AM #3
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Java uses pass by reference. When you add an object to an ArrayList, it does not create a new copy. Instead, it simply records the reference to that Object. Since there is only 1 object involved, when you change it, it changes in the ArrayList too...since it''s the same object. If you want an independent copy for your ArrayList, you'll need to create another one. I suggest looking at the 'clone' method of the Object class.
- 05-21-2010, 01:10 AM #4
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
ray = new RayTest(level, new Vector(0, 0), new Vector(600, 600));
ray.setPosition(Vector.Zero);
list.add(ray);
int max = 1;
for (int i = 0; i < max * 65; i += 65)
{
list.add(new CircleTest(level, new Vector(i, -100)));
//System.out.println( ((CircleTest)list.get(i / 65)).getPosition() );
}
Here's a sample of my code. As you can see I use the new operator for each object that is added to the list, but I'm still getting the same problem.
- 05-21-2010, 01:52 AM #5
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
You'll want to create and post a small program that is self-contained, compilable, and that we can run without modification and that demonstrates your error.
If you do post code, please be sure to use code tags so that your code retains formatting and is readable. To do this, simply surround your code with the tags:
[code]
and
[/code]
Suerte!
- 05-21-2010, 08:09 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Help! Wierd glitch invalidated int[].length method!
By soccermiles in forum New To JavaReplies: 3Last Post: 04-18-2010, 07:15 PM -
Button Glitch
By dunafrothint in forum AWT / SwingReplies: 4Last Post: 02-16-2010, 04:06 PM -
Regarding arrayList
By kishan in forum Advanced JavaReplies: 7Last Post: 08-07-2009, 12:48 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM -
ArrayList
By ramitmehra123 in forum New To JavaReplies: 1Last Post: 02-07-2008, 12:47 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks