0

我是java新手,我有一个Java Person类,我创建了一个人员列表,我可以打印该列表,但我不想打印人员列表中的最小和最大年龄我的代码是

public class Person {
    static String name;
    static String lname;
    static double age;

    Person(String name,string lname,double age) {
        Person.name = name;
        Person.lname= lname;
        Person.age = age;  
    }
}

    //Main method
public void testQuestionInput() {
    List<Person> persons = new ArrayList<Person>();
    persons.add(new Person("Foo", "scientist",5));
    persons.add(new Person("Foo", "scientist",4));
    persons.add(new Person("Foo", "teacher",10));
    persons.add(new Person("Bar", "student",11));
    persons.add(new Person("Foo", "scientist",12));

    for (Person person : _persons ) {
        System.out.print(person);
    }
}

我可以打印列表,但我想打印一个列表,在我的情况下我必须跳过最大和最小年龄我想打印列表而不

"Foo", "scientist",4 and "Foo", "scientist",12

感谢帮助将不胜感激。

4

3 回答 3

1

就目前而言,您的程序将无法运行...

public class Person {
    static String name;
    static String lname;
    static double age;

    Person(String name,string lname,double age) {
        Person.name = name;
        Person.lname= lname;
        Person.age = age;  
    }
}

的使用static意味着,根据您的示例,您的所有条目都将命名Foo scientist为年龄12

首先删除static引用...

public class Person {

    String name;
    String lname;
    double age;

    Person(String name,string lname,double age) {
        this.name = name;
        this.lname= lname;
        this.age = age;  
    }
}

接下来,您可以对列表进行排序...

Collections.sort(persons, new Comparator<Person>() {
    public int compare(Person p1, Person p2) {
        return Double.compare(p1.age, p2.age);
    }
});

然后简单地排除第一个和最后一个条目......

for (int index = 1; index < persons.size() - 2; index++) {
    Person p = persons.get(index);
    System.out.println(p.name + " " + p.lname + " @ " + p.age);
}

例如...

下一个问题是,如果您有多个具有最小或最大年龄的条目会发生什么?

对列表进行排序后,首先获取下限和上限...

int minAge = person.get(0).age;
int maxAge = person.get(person.size() - 1).age;

然后过滤掉那些不符合年龄界限的元素...

List<Person> withInLimites = new ArrayList<Person>(persons.size());
for (Person p : persons) {
    if (p.age > minAge && p.age < maxAge) {
        withInLimites.add(p);
    }
}

for (Person p : withInLimites) {
    System.out.println(p.name + " " + p.lname + " @ " + p.age);
}
于 2013-10-14T04:11:17.617 回答
0

忽略平局的可能性,您可以通过遍历列表两次来做到这一点。第一次找到最小和最大年龄,第二次打印:

public class Person {
    String name;
    String lname;
    double age;

    Person(String name, String lname, double age) {
        this.name = name;
        this.lname= lname;
        this.age = age;  
    }
}

public void testQuestionInput() {
    List<Person> persons = new ArrayList<Person>();
    persons.add(new Person("Foo", "scientist",5));
    persons.add(new Person("Foo", "scientist",4));
    persons.add(new Person("Foo", "teacher",10));
    persons.add(new Person("Bar", "student",11));
    persons.add(new Person("Foo", "scientist",12));
    double maxAge = Double.NEGATIVE_INFINITY;
    double minAge = Double.POSITIVE_INFINITY;
    for (Person person : _persons) {
        minAge = Math.min(minAge, person.age);
        maxAge = Math.max(maxAge, person.age);
    }
    for (Person person : _persons) {
        if (person.age != minAge && person.age != maxAge) {
            System.out.printf("%1$s, %2$s, %3$f%n",
               person.name, person.lname, person.age);
        }
    }
}

您可能需要toString()在类中定义一个方法Person来获得有意义的输出,System.out.print(person)就像您在当前代码中一样。

于 2013-10-14T04:04:55.320 回答
0

你的程序必须是这样的:

import java.awt.List;
import java.util.ArrayList;

public class Person {
    static String name;
    static String lname;
    static double age;

    Person(String name, String lname, double age) {
        Person.name = name;
        Person.lname = lname;
        Person.age = age;

    }

    // Main method

    public void testQuestionInput() {
        ArrayList<Person> persons = new ArrayList<Person>();
        persons.add(new Person("Foo", "scientist", 5));
        persons.add(new Person("Foo", "scientist", 4));
        persons.add(new Person("Foo", "teacher", 10));
        persons.add(new Person("Bar", "student", 11));
        persons.add(new Person("Foo", "scientist", 12));
        for (Person person : persons) {

            System.out.print(person);
        }
    }
}
于 2013-10-14T04:06:42.823 回答