简体   繁体   中英

Comparing array of objects and converting from an object to it's primitive type

I'm trying to find the lowest value of an array of object from another class and storing that object int into an int type.

I am unsure to how to compare the array values and storing the lowest value into an int.

void findlow(Student [] a) {
/* This method will find the lowest score and store it in an   array names 
lowscores. */
    for (int i = 0; i < (a.length - 1); i++) {
        for (int j = 1; i < a.length; i++) {
            if (a[i].equals(a[j])) {
                 lowscores[i] = a[i];
            }
        }
    }
}

Use lowscores[i] = a[i].(whatever integer variable); . The integer variable should be your getter or public int variable from the Student class. In java, you can't set a int variable to an object, that is why it that line is probably giving you an error.

As for finding the lowest score, get the 1st index of the a array then just check if it is less than the next index and if it is set that to a lowScore variable.

You have a void method "findlow"; this is confusing because if it's called find____ it should find something.

Break your problem into methods:

1st method - find lowest value of the object you're looking for 2nd method - put the result somewhere

so in your case you might have for the first method

public Student lowest(Student[] students) {
    Student result = students[0];
    for (Student s : students) {
        if (s.getProperty() < result.getProperty()) {
            result = s;
        }
    }
    return result;
}

and for the second

public void saveStudentInfo(Student student) {
    // do something with the student you found
}

so in total

public static void main(String[] args) {
    // ...
    Student s = lowest(students);
    saveStudentInfo(s);
}

Your question is a bit unclear and ambiguous..

I'm trying to find the lowest value of an array of object from another class and storing that object int into an int type.

First and foremost, you can easily pass the array (from the other class) to your new class.. That's pretty basic; create an object of your other class and simply reference the array.

Example: I'll assume the other class name is 'Other' and the array is named 'arr' in that class.

Other other = new Other();
Student[] student = other.arr;

Since you're doing a numerical comparison, I'll also assume your Student class has a method that returns a numerical value (say, int). Let's call this method getValue(). Now, this is how your findlow() method should be:

int findlow(Student[] a){
    int low = a[0].getValue();
    for(int i = 1; i < a.length; i++){
        if(a[i].getValue() < low){
            low = a[i].getValue();
          }
        }
}//End of method.

I hope this helps!

Merry coding!!!

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