通用的ArrayList冒泡问题 [英] Generic arraylist bubblesort problems
问题描述
我知道有内置函数,但是,作为一个学习者,我想用我自己的设备,进行排序和自排序是旧帽子,我决定尝试使自己的通用的排序例程我可以用数字或字符串,甚至日期,如果我弄清楚他们在Java中是如何工作的。
I know there are builtin routines, but as a learner, I want to sort using my own devices, and since sorting is old hat, I decided to try to make my own generic sort routine that I could use for numbers or strings and maybe even dates, if I ever figure out how they work in Java.
因此,这里是我已经,已经换了另一个是另一个错误到现在为止我只有在两个地方的错误(在**标记附后),与需要弄清楚如何比较。
So here's what I have, having traded one error for another for another until now I only have errors in two places (enclosed within "**" markers), with need to figure out how to compare.
package sort;
import java.util.ArrayList;
public abstract class Sort<E> implements Comparable<E> {
public void swap(ArrayList<E> a, int i, int j) {
E c = a.get(i);
a.set(i,a.get(j));// = a[j];
a.set(j, c);
}
public void bubbleSort(ArrayList<E> a) {
boolean inOrder = false;
while (!inOrder) {
inOrder = true;
for (int i = 1; i < a.size(); i++) {
**if( a.get(i - 1).compareTo(a.get(i)) > 0 )** {
//cannot find symbol: method compareTo(E); location: class Object
//where E is a type-variable: E extends Object declared in class Sort
inOrder = false;
swap(a, i, i - 1);
}
}
}
}
public static void main(String args[]) //hadda lose 'static' for 'setLayout' to work
{
ArrayList<Integer> ary = new ArrayList<>();
ary.add(2); ary.add(4); ary.add(7); ary.add(3);
**bubbleSort(ary)**;
//method bubbleSort in class Sort<E> cannot be applied to given types;
//required: ArrayList<E>
//found: ArrayList<Integer>
//reason: actual argument ArrayList<Integer> cannot be converted to ArrayList<E>
//by method invocation conversion where E is a type-variable:
//E extends Object declared in class Sort
for (int i = 0; i < ary.size(); i++) {
System.out.println(ary.get(i));
}
}
@Override
public int compareTo(E o) {
**return 0;** // fixing errors above may help this fall into place
}
}
我想学的东西,我的感觉的准备才发现,我没有完全准备好;密切,cigarless。
I'm trying to learn things that I feel ready for only to find that I'm not quite ready; close, cigarless.
推荐答案
这
public abstract class Sort<E> implements Comparable<E> {
意味着电子
是一个任意的对象类型,而实例排序&LT; E&GT;
可以比电子的实例
。 (所以你的错误消息,抱怨 E.compareTo
不存在,因为对象
不具备这样的。法)你想要的是这样的:
means that E
is an arbitrary object type, and that instances of Sort<E>
can be compared to instances of E
. (So your error message is complaining that E.compareTo
doesn't exist, since Object
doesn't have such a method.) What you want is this:
public abstract class Sort<E extends Comparable<E>> {
这意味着电子
必须是一个类型,其实例可以相互比较。
which means that E
must be a type whose instances can be compared to each other.
编辑补充:其实,SLaks共同指出,有一个为没有真正的理由排序
是通用的;你只需要在冒泡
方法是通用的。此外,作为MadProgrammer意味着,无论是排序
应该是非 - 摘要
(这样你就可以直接将它实例)或冒泡
应静态
(因此它可以被称为不实例化排序
实例),或者两者兼而有之。例如:
Edited to add: Actually, as SLaks together point out, there's no real reason for Sort
to be generic; you just need the bubbleSort
method to be generic. Further, as MadProgrammer implies, either Sort
should be non-abstract
(so you can instantiate it directly) or bubbleSort
should be static
(so it can be called without instantiating a Sort
instance) or both. For example:
public class Sort {
private static <E> void swap(ArrayList<E> a, int i, int j) {
...
}
private static <E extends Comparable<E>> void bubbleSort(ArrayList<E> a) {
...
}
...
}
更重要的是,排序
可以用排序
方法,以及冒泡的接口的.sort(...)
只是它(而不是让排序
特定冒泡$的实现C $ C>法)。
Better yet, Sort
can be an interface with a sort
method, and BubbleSort.sort(...)
is just an implementation of it (rather than giving Sort
a specific bubbleSort
method).
这篇关于通用的ArrayList冒泡问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!