所以,我得到了这个可比较的课程:
public class JakeInteger implements Comparable<JakeInteger>{
private int value;
public JakeInteger(int value){
this.value = value;
}
@Override
public int compareTo(JakeInteger other) {
if (other.getValue() < value)
return -1;
if (other.getValue() > value)
return 1;
return 0;
}
public String toString(){
return String.valueOf(value);
}
public int getValue(){return value;}
我想我做对了......
然后我有自己的通用对象链表:(无需阅读全部内容)
public class GenericList<Type extends Comparable> implements Iterable<Comparable>{
private Node first = null;
private int length = 0;
@Override
public Iterator<Comparable> iterator() {return new ListIterator();}
private class ListIterator implements Iterator<Comparable>{
private Node current = first;
@Override
public boolean hasNext() {return current != null;}
public Node getCurrentNode(){
return current;
}
@Override
public Comparable next() {
Comparable item = current.item;
current = current.next;
return item;
}
}
private class Node{
Comparable<Type> item;
Node next;
}
public boolean isEmpty(){
if (first == null)
return true;
return false;
}
public void prepend(Comparable item){
Node second = first;
first = new Node();
first.item = item;
first.next = second;
length++;
}
public void append(Comparable item){
if (length == 0){
prepend (item);
return;
}
Node last = first;
for (int i = 0; i<length-1; i++)
last = last.next;
last.next = new Node();
last.next.item = item;
length++;
}
public Comparable first(){
return first.item;
}
public Comparable pop(){//rest
Comparable oldFirst = first.item;
first = first.next;
return oldFirst;
}
public int getLength(){
return length;
}
// it
public Comparable getByIndex(int Index){
if (Index > this.length - 1)return null;
Iterator<Comparable> it = iterator();
for (int i = 0; i < Index-1; i++){
it.next();
}
return it.next();
}
public Node getNodeByIndex(int Index){
Node node = first;
for (int i = 0; i < Index; i++)
node = node.next;
return node;
}
public void bubbleSort(){
if (length <= 0)
return;
Node tempNode = first;
int R = length -2;
boolean swapped = true;
while (R >= 0 && swapped){
swapped = false;
for (int i = 0; i <= R; i++){
//if(tempNode.item.compareTo((Type) tempNode.next.item) )
}
}
}
private void swapWithNext(Node a){
Comparable itema = a.item;
a.item = a.next.item;
a.next.item = itema;
}
到目前为止一切正常,但现在,当我尝试实现冒泡排序时,item.compareTo()不存在。我看到Item的方式应该始终实现类似的接口并且可以访问此方法。我做错了什么?
答案 0 :(得分:3)
更改implements clause of your
GenericList`:
public class GenericList<Type extends Comparable> implements Iterable<Comparable>{
要:
public class GenericList<Type extends Comparable<Type>> implements Iterable<Type>{
仅使用Comparable
本身并不完整;你需要说出应该能够比较什么。在您的情况下,应该是类型变量Type
。
在整个课程中,您还应该将Comparable
更改为Type
,在内部课程ListIterator
中,您还应该定义一个类型变量(但为了避免混淆,请不要; t将其称为Type
,但将其称为Type2
,因为它将独立于Type
中的类型变量GenericList
public Iterator<Type> iterator() {return new ListIterator<Type>();}
private class ListIterator<Type2 extends Comparable<Type2>> implements Iterator<Type2>{
public Type2 next() {
// ...
}
// ...
public void prepend(Type2 item){
// ...
}
public void append(Type2 item){
// ...
}
public Type2 first(){
// ...
public Type2 pop(){
// ...