我有下面的代碼,不知道爲什麼有一個在list.sort錯誤Arraylist.sort要求比較
public class Ccc{
public static void main(String[] args)throws IOException,ParseException{
File inFile = new File("C:\\Users\\shyic\\Desktop\\senior_data\\s3\\s3.27.in");
Scanner in = new Scanner (inFile);
int length = Integer.parseInt(in.nextLine());
ArrayList<Integer> list = new ArrayList<>();
while(in.hasNext()){
list.add (in.nextInt());
}
list.sort();
for (int i =0;i<10000;i+=1000){
System.out.println(list.get(i));
}
}
}
錯誤:
Error:(21, 13) java: no suitable method found for sort(no arguments)
method java.util.List.sort(java.util.Comparator<? super java.lang.Integer>) is not applicable
(actual and formal argument lists differ in length)
method java.util.ArrayList.sort(java.util.Comparator<? super java.lang.Integer>) is not applicable
(actual and formal argument lists differ in length)
錯誤消息是不言自明的 - ''ArrayList'沒有'sort()'方法,但它有一個'sort(Comparator)'方法。 – assylias
使用'Collections.sort(list);' – Eran
您應該閱讀[List#sort'的文檔](https://docs.oracle.com/javase/8/docs/api/java/util/List。 html#sort-java.util.Comparator-) –