如何在java中使用Person Array中的循环查找最老的人

时间:2014-02-16 20:37:50

标签: java arrays

我是编程新手我需要编写一个代码来查找Person Array中最老的人。请帮忙。该程序已编译但未执行。它只给了我数组的大小但不是最老的人。我将不胜感激任何帮助。 我的代码如下:

import java.util.ArrayList;
public class PersonCollection {
public static void main(String[] args) {
    ArrayList<Person> aList = new ArrayList<Person>();
    // Create 5 new Person  objects and output their data
    Person person1 = new Person("Diana", "Rockman", 38, 'F', "603-28-5324");
    Person person2 = new Person("Arthur","Montgamery", 49, 'M',"402-23-5463");
    Person person3 = new Person("Kim", "Balcer", 35, 'F',"607-34-5463");
    Person person4 = new Person("Ghaffar","Kucher", 36, 'M',"537-52-6324");
    Person person5 = new Person("Zach","Boot", 19, 'M', "732-65-7364");
    aList.add(person1);
    aList.add(person2);
    aList.add(person3);
    aList.add(person4);
    aList.add(person5);
    System.out.println("The size of the list is:" + aList.size());
}
public static void oldestPerson(String[] names, int[] ages)
    {
        int index = 0;
        int oldest = ages[0];
        for ( int i=0; i < ages.length; i++)
        {

            if(ages[i] > oldest)
            {index = i;
                oldest = ages[i];
            }

            System.out.println("Person" + names[index] + "is the oldest:" +     ages    [index]);
}
}
}

3 个答案:

答案 0 :(得分:1)

使用Java 8 ...

public class PersonTest {
    public static class Person {
        public String first;
        public String last;
        public int age;
        public char gender;
        public String ssn;

        public Person(String first, String last, int age, char gender, String ssn) {
            this.first = first;
            this.last = last;
            this.age = age;
            this.gender = gender;
            this.ssn = ssn;
        }
    }

    public static void main(String[] args) {
        List<Person> aList = Arrays.asList(
                new Person("Diana", "Rockman", 38, 'F', "603-28-5324"),
                new Person("Arthur","Montgamery", 49, 'M',"402-23-5463"),
                new Person("Kim", "Balcer", 35, 'F',"607-34-5463"),
                new Person("Ghaffar","Kucher", 36, 'M',"537-52-6324"),
                new Person("Zach","Boot", 19, 'M', "732-65-7364"));

        Person oldest = aList.stream().max((a,b) -> a.age - b.age).get();
        System.out.println(String.format("The oldest person is %s %s", oldest.first, oldest.last));
    }

}

答案 1 :(得分:0)

您没有在任何地方调用oldestPerson方法(最好将其称为findOldestPerson)。此外,您应该设计该方法以List<Person>作为参数。

这可能会有所帮助:

public static void findOldestPerson(List<Person> persons) {
    Person oldest = null;
    for (Person p : persons) {
        if (oldest == null || p.getAge() > oldest.getAge()) oldest = p;
    }
    // Do something with the oldest person.
}

答案 2 :(得分:0)

System.out.println("The size of the list is:" + aList.size());之后

您必须添加对您的oldestPerson方法的调用,并且您可以通过以下方式更新您的方法:

public static void oldestPerson(ArrayList<Person> aList)
    {
        Person oldest = new Person();
        for (Person p : aList)
        {

            if(p.getAge() > oldest.getAge())
            {
                oldest = p;
            }

            System.out.println("Person" + p.getName() + "is the oldest:" +   p.getAge());
}

我使用了 getter 方法,因为我认为关于Person类的属性private,但如果它们是public你可以这样做:p.agep.name ..