Shyam Vyas
Shyam Vyas

Reputation: 61

Why is my Java Set method for arraylist not working?

public class Student implements Comparable<Student>
{
   private String name;
   private double gradePoints = 0;
   private int units = 0;
   private int index=-1;
   public Student(String name)
   {
      this.name = name;
   }
   public int getIndex() {
       return index;
   }
   public void setIndex(int i) {
       index=i;
   }
   public Student(String name, double gpa, int units)
   {
      this.name = name;
      this.units = units;
      this.gradePoints = gpa * units;

   }
   
   public String getName()
   {
      return name;
   }
   
   public double gpa()
   {
      if(units > 0) 
          return gradePoints/units;
      return 0;
   }
   
   public void addGrade(double gradePointsPerUnit, int units)
   {
      this.units += units;
      this.gradePoints += gradePointsPerUnit * units;
   }
   
   
   public int compareTo(Student other)  //Do not change this method.  Ask me why if you like.
   {
      double difference = gpa() - other.gpa();
      if(difference == 0) return 0;
      if(difference > 0) return 14;     //Do not hardcode 14, or -12, into your code.
      return -12;
   }
}

import java.util.ArrayList;

public class heapgang {
    public static void main(String[] args) {
        ArrayList<Student> x= new ArrayList<Student>();
        Student a= new Student("bob",1,2);
        Student b= new Student("arav",3,4);
        x.add(a);
        x.add(b);
        x.set(0, b); // should change the first element in the arraylist to student named "arav"
        
    }
}

At the end of my code, the first element is not change to be named arav. Why is it? I would think after the set method, both elements in the arraylist would be named "arav". I was looking all over stack overflow but I couldn't find a solution.

Here is the debugger after running code: enter image description here

Upvotes: 2

Views: 1004

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 338730

I created a simpler version of your code, omitting the distractions.

I build an ArrayList of two Student objects, one for "Bob" and one for "Alice". I replace the "Bob" object with "Alice". My list is left with two references to the same "Alice" object.

Works for me. The Answer by araqnid seems to be correct: Your debugger is either buggy or needs to be refreshed.

package work.basil.example.listing;

import java.util.ArrayList;
import java.util.List;

public class Student
{
    // member fields
    private String name;
    private double gradePoints = 0;
    private int units = 0;

    // Constructors
    public Student ( String name )
    {
        this.name = name;
    }

    public Student ( String name , double gpa , int units )
    {
        this.name = name;
        this.units = units;
        this.gradePoints = gpa * units;
    }

    // Object overrides
    
    @Override
    public String toString ( )
    {
        return "Student{ " +
                "name='" + name + '\'' +
                " | gradePoints=" + gradePoints +
                " | units=" + units +
                " }";
    }
 
    // `main` to run app
    public static void main ( String[] args )
    {
        List < Student > students = new ArrayList < Student >();
        Student bob = new Student( "Bob" , 1 , 2 );
        Student alice = new Student( "Alice" , 3 , 4 );
        students.add( bob );
        students.add( alice );
        System.out.println( "students = " + students );

        students.set( 0 , alice );
        System.out.println( "students = " + students );
    }
}

When run.

students = [Student{ name='Bob' | gradePoints=2.0 | units=2 }, Student{ name='Alice' | gradePoints=12.0 | units=4 }]
students = [Student{ name='Alice' | gradePoints=12.0 | units=4 }, Student{ name='Alice' | gradePoints=12.0 | units=4 }]

Upvotes: 0

araqnid
araqnid

Reputation: 133492

Something smells about that debugger output - it says you have the same Student object (id=48) in the list twice (as you expect) but the contents are different. Maybe the debugger hasn't entirely refreshed its display?

What happens if you loop through the contents of the list printing out the students' names?

Upvotes: 3

Related Questions