如何访问 Java 中数组对象的特定元素?
How do I access a specific element of an object of an array in Java?
此程序正在进行中。在其中,我创建了一个包含十个对象的数组,每个对象有五种不同的数据类型。我需要找到 q1 的最高分,我希望通过创建一个循环来完成,该循环将变量 highScore 与每个 q1 数据(8、3、10、8、9、7.5、8.5、6、7.5、7)进行比较作为循环经历了它的循环,但是,我收到一条错误消息,在倒数第二行显示 "The operator < is undefined for the argument type(s) double, ClassGrade" 。我不明白为什么会收到此错误消息,但我怀疑收到此错误消息的原因是我没有正确指定我试图从每个对象访问的特定元素。任何有关此事的帮助将不胜感激。
public class ClassGrade {
public String studentID;
public double q1;
public double q2;
public int mid;
public int finalExam;
public ClassGrade(String studentID, double q1, double q2, int mid, int finalExam)
{
// TODO Auto-generated constructor stub with a few modifications
}
public static void main(String[] args) {
System.out.println("this program works");
double highScore;
highScore = 0;
ClassGrade [] student = new ClassGrade[10];
student[0] = new ClassGrade ("A1", 8, 8.5, 84, 82);
student[1] = new ClassGrade ("B2", 3, 7, 0, 99);
student[2] = new ClassGrade ("C3", 10, 9.5, 92, 84);
student[3] = new ClassGrade ("D4", 8, 8, 89, 86);
student[4] = new ClassGrade ("E5", 9, 10, 83, 91);
student[5] = new ClassGrade ("F6", 7.5, 8.5, 88, 69);
student[6] = new ClassGrade ("G7", 8.5, 0, 81, 52);
student[7] = new ClassGrade ("H8", 6, 8.5, 79, 68);
student[8] = new ClassGrade ("I9", 7.5, 6, 72, 91);
student[9] = new ClassGrade ("J10", 7, 6.5, 58, 77);
for(int i=0; i<10; i++){
if (highScore < student[i])
highScore = student[i];
}
}
}
首先,您需要在构造函数中分配实例变量。
您正在比较双(高分)和 ClassGrade(学生[i])。
您需要在 ClassGrade 中创建 public 方法来 return 您想要的属性。
从数组访问对象的属性与访问单个对象的方式相同。您从数组中获取对象并使用“。”访问其 public 属性或方法。例如:
array[i].method()
您正在将高分与数组中的实际对象进行比较,您正在将学生与成绩进行比较,因此只需进行一些小改动 - 在您的 ClassGrade class 中声明一个方法,例如 getQ1()
然后从循环中访问 q1
这应该有效:
ClassGrade classGrade = (ClassGrade) student[i];
classGrade.method();
数组的每个成员仍然是一个 ClassGrade
,因此您需要做的就是像检查任何其他 ClassGrade
的 q1 一样检查其 q1
成员。
for(int i=0; i<10; i++){
if (highScore < student[i].q1)
highScore = student[i].q1;
}
就好像数组索引是名称的一部分一样,这样会更有意义。
// Consider this:
studentZero = new ClassGrade("A1", 8, 8.5, 84, 82);
if (highScore < studentZero)
highScore = studentZero;
// studentZero is not a double. Therefore...
if (highScore < studentZero.q1)
highScore = studentZero.q1;
或者,您可以为 q1
添加一个 getter,然后使用它。
int score = student[i].getQ1()
if (highScore < score)
highScore = score;
有关如何访问数组中对象成员的示例,请参阅 here:
此程序正在进行中。在其中,我创建了一个包含十个对象的数组,每个对象有五种不同的数据类型。我需要找到 q1 的最高分,我希望通过创建一个循环来完成,该循环将变量 highScore 与每个 q1 数据(8、3、10、8、9、7.5、8.5、6、7.5、7)进行比较作为循环经历了它的循环,但是,我收到一条错误消息,在倒数第二行显示 "The operator < is undefined for the argument type(s) double, ClassGrade" 。我不明白为什么会收到此错误消息,但我怀疑收到此错误消息的原因是我没有正确指定我试图从每个对象访问的特定元素。任何有关此事的帮助将不胜感激。
public class ClassGrade {
public String studentID;
public double q1;
public double q2;
public int mid;
public int finalExam;
public ClassGrade(String studentID, double q1, double q2, int mid, int finalExam)
{
// TODO Auto-generated constructor stub with a few modifications
}
public static void main(String[] args) {
System.out.println("this program works");
double highScore;
highScore = 0;
ClassGrade [] student = new ClassGrade[10];
student[0] = new ClassGrade ("A1", 8, 8.5, 84, 82);
student[1] = new ClassGrade ("B2", 3, 7, 0, 99);
student[2] = new ClassGrade ("C3", 10, 9.5, 92, 84);
student[3] = new ClassGrade ("D4", 8, 8, 89, 86);
student[4] = new ClassGrade ("E5", 9, 10, 83, 91);
student[5] = new ClassGrade ("F6", 7.5, 8.5, 88, 69);
student[6] = new ClassGrade ("G7", 8.5, 0, 81, 52);
student[7] = new ClassGrade ("H8", 6, 8.5, 79, 68);
student[8] = new ClassGrade ("I9", 7.5, 6, 72, 91);
student[9] = new ClassGrade ("J10", 7, 6.5, 58, 77);
for(int i=0; i<10; i++){
if (highScore < student[i])
highScore = student[i];
}
}
}
首先,您需要在构造函数中分配实例变量。
您正在比较双(高分)和 ClassGrade(学生[i])。
您需要在 ClassGrade 中创建 public 方法来 return 您想要的属性。
从数组访问对象的属性与访问单个对象的方式相同。您从数组中获取对象并使用“。”访问其 public 属性或方法。例如:
array[i].method()
您正在将高分与数组中的实际对象进行比较,您正在将学生与成绩进行比较,因此只需进行一些小改动 - 在您的 ClassGrade class 中声明一个方法,例如 getQ1()
然后从循环中访问 q1
这应该有效:
ClassGrade classGrade = (ClassGrade) student[i];
classGrade.method();
数组的每个成员仍然是一个 ClassGrade
,因此您需要做的就是像检查任何其他 ClassGrade
的 q1 一样检查其 q1
成员。
for(int i=0; i<10; i++){
if (highScore < student[i].q1)
highScore = student[i].q1;
}
就好像数组索引是名称的一部分一样,这样会更有意义。
// Consider this:
studentZero = new ClassGrade("A1", 8, 8.5, 84, 82);
if (highScore < studentZero)
highScore = studentZero;
// studentZero is not a double. Therefore...
if (highScore < studentZero.q1)
highScore = studentZero.q1;
或者,您可以为 q1
添加一个 getter,然后使用它。
int score = student[i].getQ1()
if (highScore < score)
highScore = score;
有关如何访问数组中对象成员的示例,请参阅 here: