简体   繁体   English

比较对象数组并从对象转换为其原始类型

[英]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. 我试图从另一个类中找到对象数组的最低值,并将该对象int存储为int类型。

I am unsure to how to compare the array values and storing the lowest value into an int. 我不确定如何比较数组值并将最小值存储到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); 使用lowscores[i] = a[i].(whatever integer variable); . The integer variable should be your getter or public int variable from the Student class. 整数变量应该是Student类中的getter或public int变量。 In java, you can't set a int variable to an object, that is why it that line is probably giving you an error. 在Java中,您不能为对象设置int变量,这就是为什么该行可能会给您一个错误。

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. 至于寻找得分最低,获得的第一个指数a数组然后就检查它是否小于下一个指标,如果它被设置为lowScore变量。

You have a void method "findlow"; 您有一个void方法“ findlow”; this is confusing because if it's called find____ it should find something. 这是令人困惑的,因为如果将其称为find____,它将找到一些东西。

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. 我试图从另一个类中找到对象数组的最低值,并将该对象int存储为int类型。

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”,并且该类中的数组名为“ arr”。

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). 由于您正在进行数值比较,因此我还将假设您的Student类具有一个返回数值的方法(例如int)。 Let's call this method getValue(). 我们将此方法称为getValue()。 Now, this is how your findlow() method should be: 现在,这就是您的findlow()方法应为:

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!!! 快活的编码!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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