-1
我想按其中包含的File
的大小對ArrayList
進行排序。ArrayList <File>按file.length排序()
public class ArraySorting implements Comparator<File> {
public ArraySorting() { }
public ArrayList<File> sort(ArrayList<File> arrayOfFiles) {
Arrays.sort(arrayOfFiles, new ArraySorting());
return arrayOfFiles;
}
public int compare(File a, File b) {
long aSize = a.length();
long bSize = b.length();
if (aSize < bSize) {
return -1;
}
else if (aSize > bSize) {
return 1;
}
else {
return 0;
}
}
}
我得到了一個錯誤:
The method sort(T[], Comparator) in the type Arrays is not applicable for the arguments (ArrayList, ArraySorting).
你使用Collections.sort(...)嗎? – nafas 2014-11-05 12:46:26
'ArrayList'是一個列表,而不是一個數組!使用'Collections.sort(arrayOfFiles,new ArraySorting())'。 – Seelenvirtuose 2014-11-05 12:46:33
您是否嘗試使用Google搜索錯誤? http://stackoverflow.com/questions/12889314/comparator-wont-work-with-arrays-sort – 2014-11-05 12:46:40