【发布时间】:2014-06-12 01:34:40
【问题描述】:
我想实现这个类来接收不同类型的数组,比如String、int、double等:
public class BubbleSort<T extends Comparable<T>> {
private T [] A;
public BubbleSort(T [] A) {
this.A = A;
}
public void bubbleSort() {
for (int i = 0; i < this.A.length; i++) {
for (int j = 0; j < this.A.length - 1; j--) {
if (this.A[j].compareTo(this.A[j - 1]) < 0) {
T aux = this.A[j];
this.A[j] = this.A[j - 1];
this.A[j - 1] = aux;
}
}
}
}
但是当我像这样创建该类的对象时:
double A[] = { 3, 2, 4, 6, 7, 1, 2, 3 };
BubbleSort bs = new BubbleSort(A);
我收到一个错误,告诉我 BubbleSort(double[]) 的构造函数未定义。我认为我在泛型类型类上做错了,我需要一些帮助。
肿块。
【问题讨论】:
-
另外,泛型不适用于原始类型。
-
@SotiriosDelimanolis 你提到的帖子不要回答我的问题
-
你为什么这么认为?你知道现在什么是原始类型吗?你明白它如何适用于你的情况吗?
-
@SotiriosDelimanolis 所以我想做什么是不可能的?
-
您可以使用
Double[]。
标签: java bubble-sort