我是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
感谢帮助将不胜感激。
答案 0 :(得分: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);
}
答案 1 :(得分: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)
的有意义输出。
答案 2 :(得分: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);
}
}
}