繁体   English   中英

具有可比较项目的通用列表Java

[英]generic list java with comparable items

因此,我得到了这个可比的课程:

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应该始终实现可比较的接口并可以使用此方法。 我究竟做错了什么?

从以下位置更改GenericList`的implements clause of your

public class GenericList<Type extends Comparable> implements Iterable<Comparable>{

至:

public class GenericList<Type extends Comparable<Type>> implements Iterable<Type>{

仅使用Comparable本身并不完整。 您需要说出它应该能够与之进行比较的地方。 在您的情况下,应为类型变量Type

纵观你的类,你也应该改变ComparableType ,并在内部类ListIterator ,你也应该定义一个类型变量(但是为了避免混淆,不要把它Type但别的东西像Type2 ,因为这将是独立于GenericList的类型变量Type

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(){
        // ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM