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 を反復処理するための List Iterator クラスを作成しました。こんな感じです。

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