0

I wrote a little program that use ArrayList collection :

import java.util.Scanner;
import java.util.ArrayList;

class MyClass {
    String name ;
    String surname ;
    int age ;
    MyClass () {}
    MyClass (String name,String surname,int age){
        this.name = name;
        this.surname= surname;
        this.age=age;
    }
    void showAll () {
        System.out.println("----------------------------------------------------------------------------------------");
        System.out.println("Name: "+name+"\n"+"Surname: "+surname+"\n"+"Age: "+age);
    }
}

public class Main {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        ArrayList<MyClass> arrayData = new ArrayList<>();
        MyClass obj= new MyClass();
        int index ;

        for (int i=1;i<=5;i++) {
            System.out.println("-------------------------------------------------------------------------------------");
            System.out.println("Data of person No "+i+" : ");
            System.out.println("Name: ");
            obj.name = input.nextLine();
            System.out.println("Surname: ");
            obj.surname = input.nextLine();
            System.out.println("Age: ");
            obj.age = input.nextInt();
            input.nextLine();
            arrayData.add(obj);
        }
        while(true) {
            System.out.println("Put index: ");
            index = input.nextInt();
            input.nextLine();
            arrayData.get(index).showAll();
        }

    }
}

But it doesn't work as expected because arrayData store only the last received object obj in all its elements.It seems there is a problem in the line :

arrayData.add(obj);

So i did some research on google and i found a way to fix the code by replacing the reference obj with an instance of Myclas like the flowing :

arrayData.add(new MyClass(obj.name, obj.surname, obj.age));

and it worked ! But i still can't understand why arrayData.add(obj); doesn't work while it seems the same thing as arrayData.add(new MyClass(obj.name, obj.surname, obj.age)); ?

  • 4
    obj already is an instance of MyClass, your entire fix makes no sense. But you are using the same variable each iteration, you should create a new instance each time you add one. Move your MyClass obj = new MyClass(); into the loop – Stultuske Oct 13 '21 at 11:35
  • 1
    It's not the same. Move your object creation `MyClass obj= new MyClass();` inside of the `for`-loop and see the difference. – jmizv Oct 13 '21 at 11:38

1 Answers1

4

You are initializing 'obj' at outside of the loop, so there is only 1 object of 'MyClass' actually. So, by adding it to ArrayList, you are trying to adding literally the same object each time and because of that, it's updating the data of all the objects inside the list which is causing your problem. Try to move 'obj = new MyClass()' to inside of the loop.

tdonuk
  • 76
  • 5
  • If what happens inside the loop is adding the same object ( the one which contains the informations inserted in the first lap ) in each lap, then why does `arrayData.get(index)` return the object which contain the informations inserted at the last lap , regardless of the index? it seems like `arrayData.add(obj);` stores the last object ( which contain the informations inserted at the last lap ) in all its elements . I want to understand what exactly happened . – Ramzi Bouali Oct 13 '21 at 15:01
  • 1
    Each time when you change the information inside one object (the last one), since all objects in the arrayList are the exactly same object, actually you are updating all of the objects inside the list because all of them are pointing the same location in the memory **(see pointers)**. So it's not makes any difference which index do you calling, all of them are equal to the last one that you have made a change. – tdonuk Oct 13 '21 at 17:39
  • This is a convincing answer .Thank you so much :) – Ramzi Bouali Oct 13 '21 at 20:38