ArrayList 未按预期打印
ArrayList not printing as expected
我有一个简单的 ArrayList
,它似乎没有按照我想要的方式打印。我有一个 class 名字 Person
如下:
public class Person {
public String Name;
public String Ni;
public String Dob;
public Person(String name, String ni, String dob){
this.Name = name;
this.Ni = ni;
this.Dob = dob;
}
public String toString()
{
return this.Name + " " + this.Ni + " " + this.Dob;
}
}
然后我只是打印列表
public static void main(String []args){
ArrayList<Person> myList = new ArrayList();
myList.add(new Person("John Smith","123456789","01/01/1990"));
myList.add(new Person("John Test","9876543211","15/05/1984"));
myList.add(new Person("Some Person","147852369","15/05/1991"));
for(Person person : myList)
{
System.out.println(person);
}
}
它按预期打印了列表,但是我试图按 Dob
下降,但我似乎无法弄清楚如何实现这一点。我在实施 Person
class 后尝试 Collections.sort
但仍然有同样的问题。
实际结果:
John Smith 123456789 01/01/1990
John Test 9876543211 15/05/1984
Some Person 147852369 15/05/1991
期望的结果:
John Test 9876543211 15/05/1984
John Smith 123456789 01/01/1990
Some Person 147852369 15/05/1991
如果有人能帮助我解决这个问题,我将不胜感激。
如果我没理解错的话,你想按出生日期排序,对吧(不是"descend")?您需要让 class 实现 Comparable
接口才能进行排序(或将 Comparator
的实现传递给排序方法)。
选项 1:
public class Person implements Comparable<Person> {
public String Name;
public String Ni;
public String Dob;
public Person(String name, String ni, String dob){
this.Name = name;
this.Ni = ni;
this.Dob = dob;
}
@Override
public int compareTo(Person person) {
return Dob.compareTo(person.getDob()); // you really want to compare by who is younger here right? whatever it is, put it here instead of String compare
}
public String getDob() {
return Dob;
}
public String toString()
{
return this.Name + " " + this.Ni + " " + this.Dob;
}
}
然后你可以调用Collections.sort(myList);
或者,使用 Collections.sort(myList, comparator)
并在那里提供一个比较器。匿名内部 class 实现的示例:
Collections.sort(myList, new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
return 0; // do your comparson here
}
});
myList.add(new Person("John Test","9876543211","15/05/1984"));
myList.add(new Person("John Smith","123456789","01/01/1990"));
myList.add(new Person("Some Person","147852369","15/05/1991"));
这样添加是因为arrayList使用索引获取数据。 0 是 John Smith,因为您首先将它添加到 list 中。当迭代 0 在 for 循环中首先打印时
public class PersonAgeComparator implements Comparator<Person>
{
public int compare(Person p1, Person p2)
{
return p1.getAge() - p2.getAge();
}
}
要调用您将使用的排序:
Collections.sort(people, new PersonAgeComparator());
你会发现下面link有用。
https://himanshugpt.wordpress.com/2010/09/10/sorting-list-in-java/
希望这对您有所帮助:)
我有一个简单的 ArrayList
,它似乎没有按照我想要的方式打印。我有一个 class 名字 Person
如下:
public class Person {
public String Name;
public String Ni;
public String Dob;
public Person(String name, String ni, String dob){
this.Name = name;
this.Ni = ni;
this.Dob = dob;
}
public String toString()
{
return this.Name + " " + this.Ni + " " + this.Dob;
}
}
然后我只是打印列表
public static void main(String []args){
ArrayList<Person> myList = new ArrayList();
myList.add(new Person("John Smith","123456789","01/01/1990"));
myList.add(new Person("John Test","9876543211","15/05/1984"));
myList.add(new Person("Some Person","147852369","15/05/1991"));
for(Person person : myList)
{
System.out.println(person);
}
}
它按预期打印了列表,但是我试图按 Dob
下降,但我似乎无法弄清楚如何实现这一点。我在实施 Person
class 后尝试 Collections.sort
但仍然有同样的问题。
实际结果:
John Smith 123456789 01/01/1990
John Test 9876543211 15/05/1984
Some Person 147852369 15/05/1991
期望的结果:
John Test 9876543211 15/05/1984
John Smith 123456789 01/01/1990
Some Person 147852369 15/05/1991
如果有人能帮助我解决这个问题,我将不胜感激。
如果我没理解错的话,你想按出生日期排序,对吧(不是"descend")?您需要让 class 实现 Comparable
接口才能进行排序(或将 Comparator
的实现传递给排序方法)。
选项 1:
public class Person implements Comparable<Person> {
public String Name;
public String Ni;
public String Dob;
public Person(String name, String ni, String dob){
this.Name = name;
this.Ni = ni;
this.Dob = dob;
}
@Override
public int compareTo(Person person) {
return Dob.compareTo(person.getDob()); // you really want to compare by who is younger here right? whatever it is, put it here instead of String compare
}
public String getDob() {
return Dob;
}
public String toString()
{
return this.Name + " " + this.Ni + " " + this.Dob;
}
}
然后你可以调用Collections.sort(myList);
或者,使用 Collections.sort(myList, comparator)
并在那里提供一个比较器。匿名内部 class 实现的示例:
Collections.sort(myList, new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
return 0; // do your comparson here
}
});
myList.add(new Person("John Test","9876543211","15/05/1984"));
myList.add(new Person("John Smith","123456789","01/01/1990"));
myList.add(new Person("Some Person","147852369","15/05/1991"));
这样添加是因为arrayList使用索引获取数据。 0 是 John Smith,因为您首先将它添加到 list 中。当迭代 0 在 for 循环中首先打印时
public class PersonAgeComparator implements Comparator<Person>
{
public int compare(Person p1, Person p2)
{
return p1.getAge() - p2.getAge();
}
}
要调用您将使用的排序:
Collections.sort(people, new PersonAgeComparator());
你会发现下面link有用。
https://himanshugpt.wordpress.com/2010/09/10/sorting-list-in-java/
希望这对您有所帮助:)