0

The following shows some test code that I am running. In the debugger I am inspecting the ArrayList "mrfThrotso" after each mrfThrotso.add(test) call.

The first two adds put two copies of the same object in and work as expected. Before the third mrfThrotso.add(test). I changed one member of the object and upon inspection of mrfThrotso, all elements have that change.

Is this how it is supposed to work?

   // 777 experimenting code
        ArrayList<structLocoLTR> mrfThrotso = new ArrayList<>();  
        structLocoLTR test = new structLocoLTR();
        structLocoLTR read = new structLocoLTR();
        Boolean bT = new Boolean( false );
        mrfThrotso.clear();

        test.Loco = "1234";
        test.nThrotNum = 0;
        test.AutoStart = "Yes";
        mrfThrotso.add( test );
        mrfThrotso.add( test );
        test.nThrotNum = 1;
        mrfThrotso.add( test );
        test.Loco = "2345";




public class structLocoLTR
{
    //
    //  
    //
    String Loco;
    String Track;
    String Role;
    String Direction;
    String Photo;
    String AutoStart;     

    //
    //  
    //
    int nThrotNum;
    int nCol;
    int nRow;
}
Tony Vink
  • 11
  • 2
  • 7
    No copies of any instance have been made. You've added the _same instance_ to the list three times, meaning the list has three references (each at a different index) to the same instance. – Slaw Jul 23 '21 at 14:47
  • 2
    The important part for you from the accepted answer of the duplicated question is the paragraph about **"Adding the Same Object"**. As explained by Slaw, you don't add copies, you add references to the same object, thus everything that refers to that index is affected by changes on that object. – Tom Jul 23 '21 at 14:55
  • Did some more experimenting and now I can see you guys are correct. Didn't doubt it but had so see for myself. Can you save me some grief and suggest a method that works like I "wish" ArrayList does? I am going to try making a new object for each add, but what happens when the object is garbage collected? Thanks, Tony – Tony Vink Jul 23 '21 at 16:11
  • [`Set`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Set.html) – Basil Bourque Jul 23 '21 at 16:22
  • Tip: Follow the Java naming conventions to make your code easier to read and comprehend. Classes start with an initial uppercase letter, variables do not. – Basil Bourque Jul 23 '21 at 16:24
  • By ne way, for something closer to a struct in Java, use the new [records](https://openjdk.java.net/jeps/395) feature in Java 16. A record is a briefer way to declare a class whose main purpose is to communicate data transparently and immutably, a nominal tuple. – Basil Bourque Jul 23 '21 at 16:28
  • "_but what happens when the object is garbage collected_" – The `ArrayList` maintains a strong reference to all its elements; as long as you maintain a strong reference to the `ArrayList` then none of its elements will be eligible for garbage collection. – Slaw Jul 23 '21 at 21:29

0 Answers0