Java-关于泛型的错误

时间:2015-04-12 03:57:08

标签: java generics interface linked-list

我有一个班级&和内部类一起工作以创建链表。节点保存的数据是Generic,我在代码行的某个地方搞砸了涉及泛型,但我不知道在哪里。我得到5个错误,第一个说“类型LinkedList不带参数”我认为这很奇怪,因为LinkedList只是我正在实现的接口。其余的都是“对象无法转换为E”。我标记了他们正在发生的地方。我认为我的任务是对的我不知道什么是错的。如果您希望我发布LinkedList接口,请告诉我。

public class SinglyLinkedList<E> implements LinkedList<E> {

    private Node head;
    private Node tail;
    private int size;

    public SinglyLinkedList(){
        this.head = null;
        this.tail = null;
        this.size = 0;
    }


     public E get(int index) {
         //Hold the data of the cur node
         //and keep iterating until you hit the
         //right index
         int hit = 0;
         Node pos = this.head;
         E found = this.head.data;//////ERROR HERE
         while (hit < index) {
             pos = pos.next;
             found = pos.data;//////ERROR HERE   
             hit++;
         }
         return found;
     }

     public void add (E data) {
         Node last = new Node(data, null);
         //If the list is empty. 
         if (this.head == null) {
             this.head = last;
         }
         //Make the cur tail's next the new node
         //and set the new node as the new tail.
         this.tail.next = last;
         this.tail = last;
         this.size++;
     }

     public boolean add(int index, E data) {
         //Cannot add if index is not valid
         if ((index >= this.size) || (index < 0) ) {
             return false;
         }
         //If index is 0, add at beginning
         Node insert = new Node(data, null);
         if (index == 0) {
             insert.next = this.head;
             this.head = insert;
             this.size++;    
             return true;
         } else { 
         //Else, go until you reach desired index then
         //Set whatever was at that index to the new Node's
         //next value.
             int hit = 1;
             Node cur = this.head;
             while (hit < index) {
                 cur = cur.next; 
                 hit++;
             }
             insert.next = cur;
             this.size++;
             return false;
         }
     }

     public void addAll(Collection<? extends E> c) {
         for (E item: c) {
             this.add(item);
         }

     }

     public boolean contains(E data) {
         int hit = 0;
         Node cur = this.head;
         Node move;
         while (hit < this.size) {
             if (data.equals(cur.data)) {
                 return true;
             } else {
                 move = cur.next;
                 cur = move;
             }
         }
         return false;
     }

     public boolean containsAll(Collection<? extends E> c) {
         for (E item: c) {
             if (!(this.contains(item))) {
                 return false;
             }
         }
         return true;
     }

     public boolean remove(E data) {
         Node prev = this.head;
         Node cur = this.head.next;
         int hit = 1;
         if (!(this.contains(data))) {
             return false;
         } else if (data.equals(head.data)) {
             this.head = cur;
             return true;
             this.size--;
         }
         while (hit < this.size) {
             if (data.equals(cur.data)) {
                 prev.next = cur.next;
                 return true;
                 this.size--;
             }
             hit++;
         }


     }

     public E remove(int index) {
         int hit = 1;
         E data;
         Node prev = this.head;
         Node cur = this.head.next;
         if (index == 0) {
             data = head.data; //////ERROR HERE
             this.head = cur;
             return data;
             this.size--;
         } else {
             while (hit < this.size) {
                 if ( hit == index) {
                     data = cur.data; //////ERROR HERE
                     prev.next = cur.next;
                     return data;
                     this.size--;
                 } else {
                     prev = cur;
                     cur = prev.next;
                     hit++;
                 }
             }
         }
     }

     public boolean removeAll(Collection<? extends E> c) {
         int prevSize = this.size;
         for (E item: c) {
             remove(item);
         }
         return (prevSize < this.size);
     }

     public int size() {
         return this.size;
     }

     public boolean isEmpty() {
         return (this.head == null);
     }

     public void clear() {
         this.head = null;
         this.tail = null;
         size = 0;
     }


    private class Node<E> {

        private E data;
        private Node next;

        private Node(E data, Node next) {
            this.data = data;
            this.next = next;
        }   
    }
}

这是接口类

/**
 * The LinkedList interface defines a set of standard operations that are
 * typically associated with linked list, such as adding, removing, and checking
 * to see if the list contains an item.
 *
 * Note that while your linked list implementation should make use of a Node
 * class, the methods below take in and return instances of the generic type,
 * not the nodes themselves.
 *
 * @author Justin Nieto
 * @version 1.1
 * @param <E> the type of the elements to store in the linked list
 */
public interface LinkedList<E> {

    /**
     * Returns the element in the linked list at the specified index.
     * Does not change the contents of the list in any way. If the given
     * index is negative or greater than the maximum possible index, returns
     * null.
     *
     * @param index of element to be retrieved
     * @return the element at the given index or null if index out of bounds
     */
    E get(int index);

    /**
     * Adds the specified piece of data to the end of the linked list.
     * This method should execute in O(1) (constant) time. This means that
     * you should not iterate over the whole list to add the item to the end
     * (we will check for this).
     *
     * @param data the object to be added to the linked list
     */
    void add(E data);

    /**
     * Adds given piece of data to the linked list at the given index.
     * All items that were originally at the index or after the index should
     * be shifted down by one. If the index specified is not valid, returns
     * false. Otherwise, returns true.
     *
     * If the index specified is 0 or if it is one larger than the maximum
     * current index (ie if index is equal to the size of the linked list),
     * then this method should execute in O(1) (constant) time. This means that
     * you should not iterate over the entire list to add the element, as it
     * is unnecessary to do so.
     *
     * @param index the index at which to add the item
     * @param data the item to be added to te linked list
     * @return true if the data could be added at the given index
     */
    boolean add(int index, E data);

    /**
     * Adds each element in the Collection to the end of the linked list.
     *
     * @param c the collection of items to add to the end of the linked list
     */
    void addAll(Collection<? extends E> c);

    /**
     * Determines whether or not the given piece of data is in the linked list.
     *
     * @param data the item to check
     * @return true if the linked list contains the item, false otherwise
     */
    boolean contains(E data);

    /**
     * Determines whether or not every element of the given Collection is
     * in the linked list.
     *
     * @param c the Collection of elements to check
     * @return true if list contains every element in the Collection
     */
    boolean containsAll(Collection<? extends E> c);

    /**
     * Finds the first element of the list equal to the given piece of data
     * and removes it from the list. Returns false if the given piece of data
     * is not in the list and therefore cannot be removed.
     *
     * @param data the piece of data to be removed from the list
     * @return true if the item was removed, false if list does not contain it
     */
    boolean remove(E data);

    /**
     * Removes and returns the item in the list at the given index.
     * All items at indices after the given index are shifted down by one.
     *
     * @param index the index of the item to remove from the linked list
     * @return the removed item
     */
    E remove(int index);

    /**
     * Removes each element in the given collection from the linked list.
     *
     * @param c the Collection of items to remove
     * @return true if each element in the Collection was removed.
     */
    boolean removeAll(Collection<? extends E> c);

    /**
     * Returns the number of elements in the linked list. This method
     * should execute in O(1) (constant) time. This means that you should not
     * iterate over the entire list to count the number of items, but rather
     * you should maintain a size variable that you can just return here.
     *
     * @return the number of elements in the linked list
     */
    int size();

    /**
     * Returns true if the linked list has no elements.
     *
     * @return true if the list is empty, false otherwise
     */
    boolean isEmpty();

    /**
     * Removes all elements from the list. After calling this method,
     * the isEmpty method should return true.
     */
    void clear();

}

1 个答案:

答案 0 :(得分:1)

NodeSinglyLinkedList的内部类,因此它已经共享了类型参数E。因此,您可以将Node设为非泛型类。

private class Node {

    private E data;
    private Node next;

    private Node(E data, Node next) {
        this.data = data;
        this.next = next;
    }   
}

这消除了许多问题,因为在Node是一个泛型类之前,你使用Node没有类型参数。

另一种解决方案是使Node成为静态嵌套类。这意味着Node的实例不属于SinglyLinkedList的实例。因此,static Node版本需要自己的类型参数。

private static class Node<E> {

    private E data;
    private Node<E> next;

    private Node(E data, Node<E> next) {
        this.data = data;
        this.next = next;
    }   
}

请注意,在此版本中,Node是通用的,因此我每次出现时都必须添加<E>。如果您采用静态方法,则必须在每次出现<E>后在课程的其余部分添加Node。这些很多

最后,消息“LinkedList不接受参数”是不言自明的。据推测,interface LinkedList应该说interface LinkedList<E>