Java 如何使用collection.sort()对集合进行排序
我正在编写一个程序来存储Java 如何使用collection.sort()对集合进行排序,java,Java,我正在编写一个程序来存储Person对象的ArrayList(输入来自文本文件) 这是Person类的代码,我将从中创建Person对象: import java.io.Serializable; public class Person implements Comparable<Person>, Serializable { private String firstName; private String lastName; private int age; public Pe
Person
对象的ArrayList
(输入来自文本文件)
这是Person
类的代码,我将从中创建Person
对象:
import java.io.Serializable;
public class Person implements Comparable<Person>, Serializable
{
private String firstName;
private String lastName;
private int age;
public Person(String firstName, String lastName, int age)
{
this.firstName = firstName;
this.lastName = lastName.toUpperCase();
this.age = age;
}
public int getAge()
{
return age;
}
public String getName()
{
return firstName;
}
/**
* @return a String of the details of a person in the format:
* Name: <firstName> <lastName> Age: <age>
*/
public String toString()
{
return
"Name: " + firstName + "" + lastName + "\t\t" + "Age: " + age;
}
/**
* Compare the age of the current instance of Person to another age of the specified Person
* @return negative number this < p
* @return 0 if this == p
* @return positive number if this > p
*/
public int compareTo(Person p) {
return ((Integer)this.getAge()).compareTo(p.getAge());
}
下面是名为Collection
的类的代码,该类将创建一个ArrayList
来存储Person
对象,因为它很长,所以代码的某些部分不相关:
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Iterator;
public class Collection
{
private ArrayList<Person> people;
public Collection()
{
people = new ArrayList<Person>();
}
public void readFromFile(String filename)
{
// code that will get input to assign values to fields to a Person
Person newPerson = new Person(firstNameToken, lastNameToken, ageToken);
}
/**
* Prints the details of each person held in the people ArrayList
*/
public void printDetails()
{
Iterator<Person> it = people.iterator();
while(it.hasNext())
{
Person p = it.next();
System.out.println(p.toString());
}
}
public static void main(String [] args) throws FileNotFoundException
{
Collection c = new Collection();
// check
//for(Person person : c.people)
//{
// System.out.println(person);
//}
Collections.sort(c.people);
}
}
import java.io.*;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.Collections;
导入java.util.List;
导入java.util.StringTokenizer;
导入java.util.Iterator;
公共类集合
{
私人ArrayList人;
公众收藏()
{
people=newarraylist();
}
public void readFromFile(字符串文件名)
{
//获取输入以将值分配给人员的字段的代码
Person newPerson=新的Person(firstNameToken、lastNameToken、ageToken);
}
/**
*打印人员列表中每个人员的详细信息
*/
公开资料
{
Iterator it=people.Iterator();
while(it.hasNext())
{
Person p=it.next();
System.out.println(p.toString());
}
}
公共静态void main(字符串[]args)引发FileNotFoundException
{
集合c=新集合();
//检查
//对于(个人:c.people)
//{
//系统输出打印项次(人);
//}
收集。分类(c.人);
}
}
但是,如果出现此错误,排序将不起作用:
线程“main”java.lang中出现异常。错误:未解决的编译问题:绑定不匹配:类型集合的泛型方法排序(列表)不适用于参数(ArrayList)。推断类型Person不是有界参数的有效替代品>
有人知道为什么吗?我正在谷歌上疯狂地寻找解决方案,我看不出我遗漏了什么。我已经实施了类似的
我创建了一个可比较的接口:public interface Comparable{public int compareTo(to);}
您不应该创建自己的接口。使用,Collections.sort()
方法期望对象实现的方法
我创建了一个可比较的接口:public interface Comparable{public int compareTo(to);}
您不应该创建自己的接口。使用,
Collections.sort()
方法期望您的对象实现的接口。好的,我删除了接口。。。现在排序终于起作用了!你能解释一下为什么我不需要创建这个界面吗?好的,我删除了这个界面。。。现在排序终于起作用了!你能解释一下为什么我不需要创建这个界面吗?
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Iterator;
public class Collection
{
private ArrayList<Person> people;
public Collection()
{
people = new ArrayList<Person>();
}
public void readFromFile(String filename)
{
// code that will get input to assign values to fields to a Person
Person newPerson = new Person(firstNameToken, lastNameToken, ageToken);
}
/**
* Prints the details of each person held in the people ArrayList
*/
public void printDetails()
{
Iterator<Person> it = people.iterator();
while(it.hasNext())
{
Person p = it.next();
System.out.println(p.toString());
}
}
public static void main(String [] args) throws FileNotFoundException
{
Collection c = new Collection();
// check
//for(Person person : c.people)
//{
// System.out.println(person);
//}
Collections.sort(c.people);
}
}