I'm trying to add a grade to my arraylist object, when I tried to add two different grades to two different arraylist objects, evetything looks okay but when I try to print both objects, it prints like both grades belongs to same object. You will understand when you see the sample execution.
我正在尝试为我的arraylist对象添加一个等级,当我尝试将两个不同的等级添加到两个不同的arraylist对象时,evetything看起来没问题但是当我尝试打印这两个对象时,它打印出两个等级都属于同一个对象。当您看到示例执行时,您将理解。
This is in my main method.
这是我的主要方法。
Student s1 = new Student("Cuneyt Cakir", "Istanbul", 35);
Student s2 = new Student("Halis Ozkahya", "Yalova", 28);
Student s3 = new Student("Mustafa Kamil Abitoglu", "Antalya", 20);
s1.addGrade(15);
s1.addGrade(89);
s1.addGrade(26);
s1.addGrade(44);
s2.addGrade(33);
System.out.println(s1);
System.out.println(s2);
The result is:
结果是:
Name: Cuneyt Cakir / City: Istanbul / Age: 35
Grades: [15, 89, 26, 44, 33]
Name: Halis Ozkahya / City: Yalova / Age: 28
Grades: [15, 89, 26, 44, 33]姓名:Cuneyt Cakir /城市:伊斯坦布尔/年龄:35年级:[15,89,26,44,33]姓名:Halis Ozkahya /城市:Yalova /年龄:28年级:[15,89,26,44,33]
This is my add method in Students class:
这是我在Students课程中的添加方法:
public static void addGrade(int grade) {
if(grade>=0 && grade<=100){
grades.add(grade);
} else
System.out.println("Invalid grade.");
}
This is another part of Student class:
这是Student class的另一部分:
public static ArrayList<Integer> grades=new ArrayList<Integer>();
public Student(String value1, String value2, int value3) {
name=value1;
city=(value2);
age=(value3);
}
And I'm getting grades with this code:
我正在用这段代码获得成绩:
public ArrayList<Integer> getGrades() {
return grades;
}
The expected output is:
预期的产出是:
Name: Cuneyt Cakir / City: Istanbul / Age: 35
Grades: [15, 89, 26, 44]姓名:Cuneyt Cakir /城市:伊斯坦布尔/年龄:35年级:[15,89,26,44]
Name: Halis Ozkahya / City: Yalova / Age: 28
Grades: [33]姓名:Halis Ozkahya /城市:Yalova /年龄:28年级:[33]
2 个解决方案
#1
Its because grades
is a static variable. Remove 'static' keyword to create a new ArrayList instance for each object.
因为成绩是一个静态变量。删除'static'关键字为每个对象创建一个新的ArrayList实例。
Static variables in a class are common to all objects. So `grades' variable is common to all S1, S2 and S3 instances.
类中的静态变量对所有对象都是通用的。所以`等级'变量对于所有S1,S2和S3实例都是通用的。
#2
Remove static modifier,
删除静态修改器,
public static ArrayList<Integer> grades=new ArrayList<Integer>();
Change to,
public ArrayList<Integer> grades=new ArrayList<Integer>();
Static modifier makes "grades" a class member. Removing static makes it an instance member
静态修饰符使“成绩”成为类成员。删除静态使其成为实例成员
#1
Its because grades
is a static variable. Remove 'static' keyword to create a new ArrayList instance for each object.
因为成绩是一个静态变量。删除'static'关键字为每个对象创建一个新的ArrayList实例。
Static variables in a class are common to all objects. So `grades' variable is common to all S1, S2 and S3 instances.
类中的静态变量对所有对象都是通用的。所以`等级'变量对于所有S1,S2和S3实例都是通用的。
#2
Remove static modifier,
删除静态修改器,
public static ArrayList<Integer> grades=new ArrayList<Integer>();
Change to,
public ArrayList<Integer> grades=new ArrayList<Integer>();
Static modifier makes "grades" a class member. Removing static makes it an instance member
静态修饰符使“成绩”成为类成员。删除静态使其成为实例成员