0

我正在编写一个列表 ADT,与(堆栈和队列)不同,我将能够在列表中的任何位置添加/删除项目。我还希望能够遍历列表。所以我正在创建指针来跟踪项目。当我尝试在列表中添加项目(在测试工具类中)时,我得到一个绑定的不匹配。
请看一下下面的界面,我想您将能够获得更好的主意。

package List;

public interface ListInterface < ObjectType extends KeyInterface > extends Iterable<ObjectType> {

    public void add ( ObjectType item );    // adds item in front of the key

    public ObjectType remove ( );           // returns item at the key and removes it

    public ObjectType get ( );              // returns item at the key

    public boolean empty ( );               // returns boolean // check for empty

    public int length ( );                  // length of the list

    public void toFront ( );                // moves pointer to the front of the list 

    public void advance( );                 // moves the pointer to the next item 

    public void find ( String key );        // pointer finds the input key 

    public boolean offEnd ( );              // returns true if pointer is off the list

}

使用有界类型参数使 ObjectType 成为KeyInterface 下面的子类型是 KeyInterface

package List;

public interface KeyInterface {

    public String getKey ( ); // Returns the Key of the item
}

这是我的 List 实现的样子。

package List;

import java.util.Iterator;

public class ListImplementation < ObjectType extends KeyInterface > implements ListInterface<ObjectType> {


    ObjectType[]  items;   // the items in the list
    int  cursor;  // the list cursor
    int  length;  // the length of the list


    public ListImplementation() {
        this(100);
    }// constructor 


    public ListImplementation(int size) {
         items = (ObjectType[]) new KeyInterface[size];
         cursor = 0;
         length = 0;
    }// constructor


    @Override
    public Iterator<ObjectType> iterator() {
         return new ListIterator<ObjectType>(this);
    }

    @Override
    public void add(ObjectType item) {
    int  j;

        if ( length >= items.length ) {
            throw new NullPointerException();
        }
        else {
            for ( j = length-1 ; j>=cursor ; j-- ) {
                items[j+1] = items[j];
            }
            items[cursor] = item;
            length = length + 1;
        }
    }
    @Override
    public ObjectType remove() {
        ObjectType    i;
        int  j;

        if ( cursor >= length ) {
            throw new NullPointerException();
        }
        else {
            i = items[cursor];
            for ( j=cursor+1 ; j<length ; j++ ) {
                items[j-1] = items[j];
            };
            length = length-1;
            items[length] = null;
            return i;
        }
    }
    @Override
    public ObjectType get() {
           if ( cursor >= length ) {
                throw new NullPointerException();
            }
            else {
                return items[cursor];
            }
    }

    @Override
    public boolean empty() {
         return length == 0;
    }

    @Override
    public int length() {
        return length;
    }

    @Override
    public void toFront() {
        cursor = 0;

    }


    @Override
    public void advance() {

        if ( cursor < length ) {
            cursor = cursor + 1;
        }
    }

    @Override
    public void find(String key) {
        while ( cursor < length && key.compareTo(items[cursor].getKey()) != 0 ) {
            cursor = cursor + 1;
        }

    }
    @Override
    public boolean offEnd() {
        return cursor >= length;
    }

}

从逻辑上讲,我在这个类中看不到任何错误,并且我已经分别测试了这些方法。还制作了一个 List Iterator 类来遍历 List。这就是它的样子。

public class ListIterator<ObjectType extends KeyInterface > implements Iterator<ObjectType> {

    private int         cursor;                       // the cursor that iterates through the list
    private ListImplementation <ObjectType>  list;    // the list being iterated over

    ListIterator ( ListImplementation <ObjectType> l ) {

        list = l;
        cursor = 0;

    };  // constructor

    @Override
    public boolean hasNext() {
        return cursor < list.length;
    }
    @Override
    public ObjectType next() {
         ObjectType  i;

            if ( cursor >= list.length ) {
                throw new NoSuchElementException();
            }
            else {
                i = list.items[cursor];
                cursor = cursor + 1;
                return i;
            }
    }
    @Override
    public void remove() {
         throw new UnsupportedOperationException(); // Doesn't do anything
    }



}

我知道 remove 方法没有做任何事情,因为它的设计很糟糕,请暂时忽略它。现在我被困在测试线束部分。我不知道如何添加不同的项目,因为类中有很多扩展。

public static void main(String args[]){
    ListImplementation < String> ls = new ListImplementation < String>();
}

无论我放在里面什么<>结果都是有界的不匹配。所以什么可以是有界参数的有效替代品。帮助??

4

1 回答 1

0

您已将列表定义为

ListImplementation<ObjectType extends KeyInterface>

因此,由于泛型类型 extends KeyInterface,您只能创建 的列表KeyInterface或 的子类型KeyInterface

String 没有实现KeyInterface,这就是为什么你不能创建 String 的 ListImplementation。

于 2014-03-22T09:32:30.527 回答