【发布时间】:2016-06-20 23:15:14
【问题描述】:
我正在学习 Java 并编写 QuickSort 类。在某些时候需要交换数组中的元素,所以我尝试使用Collections.swap 来做这件事,例如in this question。但是,javac QuickSort.java 向我抛出了一个错误:
error: swap(Object[],int,int) has private access in Collections
我在这里做错了什么? QuickSort.java的完整代码:
package src.sort;
import java.util.Collections;
public class QuickSort {
public static void sort(Comparable[] xs) {
sort(xs, 0, xs.length);
}
private static boolean less(Comparable x, Comparable y) {
return x.compareTo(y) < 0;
}
private static void sort(Comparable[] xs, int fst, int lst) {
if (fst >= lst) return;
int i = fst, j = lst;
Comparable pivot = xs[(i + j) / 2];
while (i <= j) {
while (less(xs[i++], pivot));
while (less(pivot, xs[j--]));
if (i <= j) Collections.swap(xs, i++, j--);
}
sort(xs, fst, j);
sort(xs, i, lst);
}
}
【问题讨论】:
标签: java collections compiler-errors