我在寫一個對二進制文件起作用的排序方法。 (在文件內部完成的所有操作,無需將整個文件讀入內存)。Java:通過一個比較器來執行我的qsort
我得到: 「比較器類型中的方法compare(E,E)不適用於來自qsort方法的參數(so,so)」 。
你能明白爲什麼嗎?
謝謝!
代碼:
class so {
// Some object
}
class compareByIntValue implements Comparator<so> {
public int compare(so o1, so o2) {
// Comparing
}
}
class StructureOnFile {
public static void main(String[] args) throws IOException {
RandomAccessFile objectsOnFile = new RandomAccessFile(FILENAME, "rw");
fillWithObjects(objectsOnFile);
quickSort(objectsOnFile, RecordSize, new compareByIntValue());
.
.
.
}
static <E> void quickSort(RandomAccessFile raf,
int RecordSize, Comparator<E> c) throws IOException {
quickSort(raf, RecordSize, c, 0, (int) (raf.length() - RecordSize));
}
static <E> void quickSort(RandomAccessFile raf,
int RecordSize, Comparator<E> c, int first, int last) {
if (last > first) {
int pivotIndex = partition(raf, RecordSize, first, last, c);
quickSort(raf, RecordSize, c, first, pivotIndex - RecordSize);
quickSort(raf, RecordSize, c, pivotIndex + RecordSize, last);
}
}
static <E> int partition(RandomAccessFile list,
int RecordSize, int first, int last, Comparator<E> c) {
so pivot = new so();
so tmp = new so();
list.seek(first);
pivot.readFromFile(list); // Reads first element into pivot
int low = first + RecordSize; // Index for forward search
int high = last; // Index for backward search
while (high > low) {
// Search forward from left
list.seek(low);
tmp.readFromFile(list);
HERE =================> while (low <= high && c.compare(tmp, pivot) <= 0)
low+=RecordSize;**