Hi guys, I am toying around with ArrayLists and objects and I created a quick program to add some objects to an ArrayList and print the list afterwards. I ran into an issue, though. I am overriding the toString method in order to display the names of the objects I input as oppsoed to their addresses. When I input one object, it goes into the arraylist fine, but when I input a second it seems to replace the first. Here's the code and an example run of the program:

package studenttest;

import java.util.*;

public class Studenttest {

    private static final int MAX_STUDENTS = 2;

    public static void main(String[] args) {

        ArrayList<Stuclass> stuList = new ArrayList<Stuclass>();
        Stuclass stu = new Stuclass();
        Scanner keyboard = new Scanner(System.in);

        String stuName;
        int stuCurrGrade;
        int stuFinalGrade;


        for(int i = 0; i< MAX_STUDENTS; i++){
            System.out.println("Enter student's name: ");
            stuName = keyboard.nextLine();
            stu.setName(stuName);

            System.out.println("Enter student's current grade: ");
            stuCurrGrade = keyboard.nextInt();
            stu.setCurrGrade(stuCurrGrade);

            stuList.add(stu);

            keyboard.nextLine();           
        }

        System.out.println(stuList);
    }
}

package studenttest;


public class Stuclass {
    //Field Declarations
    private int currGrade;
    private int finalGrade;

    private String name;

    //Constructor
    public Stuclass() {

    }

    @Override
    public String toString(){
        return name;
    }
    //----------------Get/Set methods----------------------
    public void setCurrGrade(int cG){
        currGrade = cG;
    }

    public void setFinalGrade(int fG){
        finalGrade = fG;
    }

    public void setName(String n){
        name = n;
    }

    public int getCurrGrade(){
        return currGrade;
    }

    public int getFinalGrade(){
        return finalGrade;
    }

    public String getName(){
        return name;
    }
   //--------------------------------------------------------


}

Very simple stuff. Here's a sample run:

 run:
Enter student's name: 
John
Enter student's current grade: 
88
Enter student's name: 
Joe
Enter student's current grade: 
87
[Joe, Joe]
BUILD SUCCESSFUL (total time: 9 seconds)

It should display [Joe, John] as opposed to [Joe, Joe]. I'm a little confused, but the answer is probably very simple. Thanks!

What you are doing is changing the contents of the Student object and adding the same object reference to the arraylist so you end up with the arraylist containing references to the one and only Student class object whose contents are set to the last values. Make a new Student object for the new data.

I knew it was something simple! Thanks a lot, not sure why I missed that.

Works fine now, here's the revised (relevent) code:

for(int i = 0; i< MAX_STUDENTS; i++){
            Stuclass stu = new Stuclass();

            System.out.println("Enter student's name: ");
            stuName = keyboard.nextLine();
            stu.setName(stuName);

            System.out.println("Enter student's current grade: ");
            stuCurrGrade = keyboard.nextInt();
            stu.setCurrGrade(stuCurrGrade);

            stuList.add(stu);

            keyboard.nextLine();           
        }
This question has already been answered. Start a new discussion instead.