0

I'm writing a List ADT where I will be able to add/remove items anywhere in the list unlike (Stacks and Queues). I also want to be able to iterate through the list. So I'm creating pointers to keep track of items.When I try to add items in the list (in test harness class) I get a bound miss-match.
Please take a look at the interface below I think you will be able to get a better idea.

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

}

Using a bounded type parameter to make ObjectType a subtype of KeyInterface Below is the KeyInterface

package List;

public interface KeyInterface {

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

Here's how my List implementation looks like.

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;
    }

}

Logically I don't see any errors in this class and I have tested the methods separately. Also made a List Iterator class to for iterating through the List. This is how it looks like.

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
    }



}

I know the remove method is doing nothing a its a bad design, please ignore it for now. Now I'm stuck in the test Harness part. I don't know how can I add different items since there are so many extends in class.

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

whatever I put inside <> turns out to be a bound mismatch.so what could be a valid substitute for the bounded parameter. Help??

Shadid
  • 4,133
  • 9
  • 30
  • 53

1 Answers1

0

You've defined the list as

ListImplementation<ObjectType extends KeyInterface>

So, since the generic type extends KeyInterface, you can only create lists of KeyInterface, or subtypes of KeyInterface.

String doesn't implement KeyInterface, and that's why you can't create a ListImplementation of String.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255