我编写了一个程序,在其中将对象添加到数组列表中,然后按 ID 对其进行排序。这是我的程序:
package TestPackage;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import com.fulcrum.emp.EmployeeSortById;
public class EmployeeSorting {
// path till 'employee files' folder.
File folder = new File("D:\\Arthi iyer\\employee files");
// listFiles() : list all the files in a folder.
File[] listOfFiles = folder.listFiles();
ArrayList<Employee> emp = new ArrayList<Employee>();
String[] split_input = null;
public void sortFiles() throws FileNotFoundException {
for (File file : listOfFiles) {
Scanner scanner = new Scanner(file);
String input = scanner.nextLine();
split_input = input.split("=");
int id = Integer.parseInt(split_input[1]);
String input1 = scanner.nextLine();
split_input = input1.split("=");
String name = split_input[1];
String input2 = scanner.nextLine();
split_input = input2.split("=");
int age = Integer.parseInt(split_input[1]);
// Employee e=new Employee(id, name, age);
// System.out.println(e);
emp.add(new Employee(id, name, age));
// System.out.println(emp.size());
// for(int i=0;i<emp.size();i++)
// {
// System.out.println(emp.get(i) +""+i);
// }
}// for ends
}// method ends
public void sortByID() {
System.out.println("----Sort By Employee Id----");
Collections.sort(emp, new EmployeeSortById());
}
public static void main(String[] args) throws FileNotFoundException {
EmployeeSorting sort = new EmployeeSorting();
sort.sortFiles();
sort.sortByID();
}
}
我的问题是它给出了一个错误说:
Collections 类型中的方法 sort(List, Comparator) 不适用于参数 (ArrayList, EmployeeSortById)
但在我的EmployeeSortById
课堂上,我已经Comparator<Employee>
正确实施了。仍然存在问题。谁能指导我?