Java 如何比较3个以上的对象?

Java 如何比较3个以上的对象?,java,Java,我有一个由3个或更多Person对象组成的数组,我正在研究如何比较每个对象,以找到“最好的”最年轻和最高的人 public class Person { private int age; private int height; public int Person( int age, int height ) { this.age = age; this.height = height; } public int get

我有一个由3个或更多Person对象组成的数组,我正在研究如何比较每个对象,以找到“最好的”最年轻和最高的人

public class Person {

    private int age;
    private int height;

    public int Person( int age, int height ) {
        this.age = age;
        this.height = height;
    }

    public int getAge() { return age; }
    public int getHeight() { return height; }
}
在我的实现中,我在数组中添加Person对象,如下所示:

Array<Person> persons = new Array<Person>();
persons.add( new Person(28, 150) );
persons.add( new Person(38, 155) );
persons.add( new Person(18, 160) );
Array persons=new Array();
增加(新的人员(28150));
新增(新增人员(38155人));
增加(新的人员(18,160));
给定数组,如何比较迭代中的persons对象

for( int i=0; i<persons.size; i++ ) {
    Person person = persons.get(i);

    // compare age and height?
}

for(inti=0;i您可以排序,但我怀疑您只是想要这个

Person youngest = null;
Person tallest = null;
int lowest_age = 0;
int tallest_height = 0;

for( int i=0; i<persons.size; i++ )
{
    Person person = persons.get(i);
    int age = person.getAge();
    int height = person.getHeight();

    if ((age < lowest_age || (youngest == null))
    {
       lowest_age = age;
       youngest = person;
    }

    if ((height > tallest_height || (tallest == null))
    {
       tallest_height = height;
       tallest = person;
    }
}
Person=null;
Person=null;
int最低年龄=0;
int_高度=0;
对于(int i=0;i最高的高度| |(highter==null))
{
最高高度=高度;
最高的=人;
}
}

尝试使用类似的界面,如下所示:

import org.apache.commons.lang.builder.CompareToBuilder;

public class Person implements Comparable<Person> {

    private int age;
    private int height;

    public int Person( int age, int height ) {
        this.age = age;
        this.height = height;
    }

    public int getAge() { return age; }
    public int getHeight() { return height; }

    public int compareTo(Person other) { 
        return new CompareToBuilder().append(age, other.age).append(height, other.height).toComparison(); 
    }
}
public int compareTo(Person other) { 
    return new CompareToBuilder().append(age, other.age).append(other.height, height).toComparison();
}
注意:如果要按降序排序,只需切换其他对象的内部属性和属性,如下所示:

import org.apache.commons.lang.builder.CompareToBuilder;

public class Person implements Comparable<Person> {

    private int age;
    private int height;

    public int Person( int age, int height ) {
        this.age = age;
        this.height = height;
    }

    public int getAge() { return age; }
    public int getHeight() { return height; }

    public int compareTo(Person other) { 
        return new CompareToBuilder().append(age, other.age).append(height, other.height).toComparison(); 
    }
}
public int compareTo(Person other) { 
    return new CompareToBuilder().append(age, other.age).append(other.height, height).toComparison();
}

我怀疑您正在寻找如何对数组进行排序?实现
comparable
comparator
接口并调用
sort()
,根据您的实现,您将得到排序数组/列表中的第一个人。不是答案,公共整数个人(整数年龄,整数高度)方法没有返回语句,或者将其写入constructor@Tirath很好的捕捉。我的思维是C++的。固定的!我想这就是我应该做的。我刚刚安装了ApChan.Mux.Langand现在工作得很好。谢谢大家,谢谢大家的帮助。欢迎你。如果你认为是B,请选择我的答案。单击我的答案旁边的勾号图标来确定答案。祝您度过愉快的一天:-)