简体   繁体   中英

How do I access a specific element of an object of an array in Java?

This program is a work in progress. In it, I created an array of ten objects with five pieces of different data types for each object. I need to find the highest score for q1 which I hoped to accomplish by creating a loop that would compare the variable highScore with each q1 data(8, 3, 10, 8, 9, 7.5, 8.5, 6, 7.5, 7) as the loop went through its cycles, however, I am getting an error message that says "The operator < is undefined for the argument type(s) double, ClassGrade" at the line that is second from the bottom. I don't understand why I am getting this error message, but i suspect that the reason i am getting it is that I am not correctly specifying the specific element that i am trying to access from each object. Any help on the matter would be greatly appreciated.

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];

   }




}

}

First, you need to assign your instance variables in you constructor.

You are comparing a double (highscore) with a ClassGrade (student[i]).

You need to create public methods in ClassGrade to return your desired properties.

Accessing an object's properties from an array is just the same way as from a single object. You fetch the object from the array and use '.' to access its public properties or methods. Eg:

array[i].method()

您正在将高分与数组中的实际对象进行比较,将学生与年级进行比较,因此只需做一些小改动-在ClassGrade类中声明一个方法,如getQ1() ,然后从循环中访问q1

This should work:

ClassGrade classGrade = (ClassGrade) student[i];
classGrade.method();

Each member of the array is still a ClassGrade , so all you need to do is check its q1 member like you would any other ClassGrade 's q1.

for(int i=0; i<10; i++){
    if (highScore < student[i].q1)
        highScore = student[i].q1;
}

Just think about it as if the array index is part of the name, and it'll make more sense.

// 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;

Alternatively, you can add a getter for q1 , and use it.

int score = student[i].getQ1()
if (highScore < score)
    highScore = score;

See here for an example of how to access a member of an object in an array:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM