Results 1 to 6 of 6
  1. #1
    pghazanfari is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default ArrayList glitch?

    Okay, so I have an arraylist of a custom class I made. Everytime i try to set the variable of one of the classes to something, all the object's variables in the arraylist change to that as well. Can anyone help?

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    well, are you creating new instances of the object for each adding to the list.

    for example,
    Java Code:
    MyObject myObj = new MyObject();
    
    List<MyObject> aList = new ArrayList<MyObj>();
    
    aList.add(myObj);
    aList.add(myObj);
    aList.add(myObj);
    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.

    Instead,
    Java Code:
    List<MyObject> aList = new ArrayList<MyObj>();
    
    aList.add(new MyObject());
    aList.add(new MyObject());
    aList.add(new MyObject());
    would create three different object references into the list.

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    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.

  4. #4
    pghazanfari is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    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.

  5. #5
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    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:
    [cod&#101;]
    and
    [/cod&#101;]
    Suerte!

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by toadaly View Post
    Java uses pass by reference.
    Ahem: s/reference/value/

    kind regards,

    Jos

Similar Threads

  1. Help! Wierd glitch invalidated int[].length method!
    By soccermiles in forum New To Java
    Replies: 3
    Last Post: 04-18-2010, 07:15 PM
  2. Button Glitch
    By dunafrothint in forum AWT / Swing
    Replies: 4
    Last Post: 02-16-2010, 04:06 PM
  3. Regarding arrayList
    By kishan in forum Advanced Java
    Replies: 7
    Last Post: 08-07-2009, 12:48 PM
  4. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  5. ArrayList
    By ramitmehra123 in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 12:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •