-3

I've recently been learning Java. So basically I created a Student Model class which consists of student's info along with their marks info. And then I created a Student Main Class where I created an object of type Student Model Class. I wanted to create a map of type Map<StudentModelClass,ArrayList<Integer>.

I want to store student details as a key and only their marks which is a list as value in a map. I wrote the code but I don't know why is my map taking all the values in map entries the same marks.

This is my Student Model Class

package com.src.StudentDetailsAsMap;

public class StudentDetails {

    private int stdid;
    private String name;
    private int age;
    private String mobileNumber; 
    private int m1,m2,m3;
    private int total_marks_secured=0;
    private double avg_marks_secured=0.0;

    
    
    public StudentDetails(int stdid, String name, int age, String mobileNumber, int m1, int m2, int m3) {
        this.setStdid(stdid);
        this.setName(name);
        this.setAge(age);
        this.setMobileNumber(mobileNumber);
        this.setM1(m1);
        this.setM2(m2);
        this.setM3(m3);
        this.setTotal_marks_secured(m1+m2+m3);
        this.setAvg_marks_secured(this.getTotal_marks_secured()/3);
    }
    public StudentDetails() {
    }
    public int getStdid() {
        return stdid;
    }
    public void setStdid(int stdid) {
        this.stdid = stdid;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getMobileNumber() {
        return mobileNumber;
    }
    public void setMobileNumber(String mobileNumber) {
        this.mobileNumber = mobileNumber;
    }
    public int getM1() {
        return this.m1;
    }
    public void setM1(int m1) {
        this.m1 = m1;
    }
    public int getM2() {
        return this.m2;
    }
    public void setM2(int m2) {
        this.m2 = m2;
    }
    public int getM3() {
        return this.m3;
    }
    public void setM3(int m3) {
        this.m3 = m3;
    }
    public int getTotal_marks_secured() {
        return total_marks_secured;
    }
    public void setTotal_marks_secured(int total_marks_secured) {
        this.total_marks_secured = total_marks_secured;
    }
    public double getAvg_marks_secured() {
        return avg_marks_secured;
    }
    public void setAvg_marks_secured(double avg_marks_secured) {
        this.avg_marks_secured = avg_marks_secured;
    }
    
    
    public void display() {
        System.out.println("Student [stdid=" + stdid + ", name=" + name + ", age=" + age + ", mobileNumber=" + mobileNumber
                + ", m1=" + m1 + ", m2=" + m2 + ", m3=" + m3 + ", total_marks_secured=" + total_marks_secured
                + ", avg_marks_secured=" + avg_marks_secured + "]");
    }
    
}

This is my Student Main Class

 package com.src.StudentDetailsAsMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class StudentMap {
    public static void main(String[] arg) {
        StudentDetails obj = new StudentDetails();
        ArrayList<Integer> marks = new ArrayList<Integer>();
        Map<StudentDetails,ArrayList<Integer>> mp = new HashMap<StudentDetails,ArrayList<Integer>>();
        
        obj = new StudentDetails(1,"kevin",20,"23425",89,98,89);
        marks.add(obj.getM1());
        marks.add(obj.getM2());
        marks.add(obj.getM3());
        System.out.println("marks1: " + marks.get(0) + " " + marks.get(1) +" " + marks.get(2));
        mp.put(obj,marks);
        
        marks.clear();
        obj = new StudentDetails(2,"fred",20,"12345",81,98,89);
        marks.add(obj.getM1());
        marks.add(obj.getM2());
        marks.add(obj.getM3());
        System.out.println("marks2 " + marks.get(0) + " " + marks.get(1) +" " + marks.get(2));
        mp.put(obj,marks);
        
        obj = new StudentDetails(3,"rue",20,"24564",89,56,89);
        marks.clear();
        marks.add(obj.getM1());
        marks.add(obj.getM2());
        marks.add(obj.getM3());
        mp.put(obj,marks);
        
        
        obj = new StudentDetails(4,"paxton",20,"85756",89,35,89);
        marks.clear();
        marks.add(obj.getM1());
        marks.add(obj.getM2());
        marks.add(obj.getM3());
        mp.put(obj,marks);
        
        
        for(Entry mp1 : mp.entrySet()) {
            StudentDetails s = (StudentDetails) mp1.getKey();
            System.out.println("Key: ");
            s.display();
            System.out.println("Value: ");
            ArrayList<Integer> arr = (ArrayList<Integer>)mp1.getValue();
            System.out.println(arr);
        }
        
        
    }
    
}

This is the output I got

newbie
  • 19
  • 6

1 Answers1

2

The reason is because your array list is the same object from beginning to end.

The ArrayList that your Map<StudentDetails,ArrayList> mp stores are all pointing to same object's memory address, therefore, you will always have same value being print out from your output.

Like @nanofarad said, construct a new ones like you did to StudentDetails obj

Gary Liao
  • 61
  • 6
  • 1
    You can try to verify by not to add anything into marks after you do marks.clear(). See what you get from the outputs. – Gary Liao Oct 08 '22 at 14:51
  • 1
    I think you can have a good use of this [primitive-and-reference-variables](https://java-programming.mooc.fi/part-5/3-primitive-and-reference-variables) – Gary Liao Oct 08 '22 at 15:06