3 개 이상의 Person 객체로 구성된 배열이 있는데, 각각을 비교하여 가장 젊고 가장 키가 큰 사람을 찾는 방법을 알아 냈습니다. 내 구현에서3 개 이상의 개체를 비교하는 방법은 무엇입니까?
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; }
}
, 정말 같은 배열에있는 사람의 개체를 추가 :
Array<Person> persons = new Array<Person>();
persons.add(new Person(28, 150));
persons.add(new Person(38, 155));
persons.add(new Person(18, 160));
배열을 감안할 때
가 어떻게이 사람을 비교합니까 객체 반복에서?for(int i=0; i<persons.size; i++) {
Person person = persons.get(i);
// compare age and height?
}
배열을 정렬하는 방법을 찾으셨습니까? –
은 'comparable'또는 'comparator'인터페이스를 구현하고 구현에 따라'sort() '를 호출하면 정렬 된 배열/목록에서 맨 처음 사람을 얻을 수 있습니다. – TheLostMind
답변 없음, public int Person (int age, int height) 메서드 return 문이 없거나 생성자로 작성하십시오. – Anptk