“链接列表无法找到符号 - 方法添加”错误

时间:2016-01-27 14:42:51

标签: singly-linked-list

我正在尝试使用add方法将元素添加到LinkedList。这就是代码的样子:

       /**
 * A singly linked list.
 * 
 * @author 
 * @version
 */
public class LinkedList<T> { 
    private ListElement<T> first;   // First element in list.
    private ListElement<T> last;    // Last element in list.
    public LinkedList<T> asd;
    private int size;               // Number of elements in list.

    /**
     * A list element.
     */
    private static class ListElement<T> {
        public T data;
        public ListElement<T> next;

        public ListElement(T data) {
            this.data = data;
            this.next = null;
        }
    }

    /**
     * This TEST METHOD returns true if the following invariants hold:
     * <ul>
     *   <li> size equals the number of list elements, </li>
     *   <li> if size == 0, first == null and last == null, </li>
     *   <li> if size > 0, first != null and last != null, </li>
     *   <li> if size == 1, first == last, </li>
     *   <li> last.next == null. </li>
     * </ul>
     */
    public boolean isHealthy() {
        return false;
    }

    /**
     * Creates an empty list.
     */
    public LinkedList() {
        asd = new LinkedList<T>();
    }

    /**
     * Inserts the given element at the beginning of this list.
     */
    public void addFirst(T element) {
        asd.add(element);
    }

    /**
     * Inserts the given element at the end of this list.
     */
    public void addLast(T element) {
    }

    /**
     * Returns the first element of this list.
     * Returns <code>null</code> if the list is empty.
     */
    public T getFirst() {
        // TODO
        return null;
    }

    /**
     * Returns the last element of this list.
     * Returns <code>null</code> if the list is empty.
     */
    public T getLast() {
        return null;
    }

    /**
     * Returns the element at the specified position in this list.
     * Returns <code>null</code> if <code>index</code> is out of bounds.
     */
    public T get(int index) {
        return null;
    }

    /**
     * Removes and returns the first element from this list.
     * Returns <code>null</code> if the list is empty.
     */
    public T removeFirst() {
        return null;
    }

    /**
     * Removes all of the elements from this list.
     */
    public void clear() {
    }

    /**
     * Returns the number of elements in this list.
     */
    public int size() {
        return 0;
    }

    /**
     * Returns <code>true</code> if this list contains no elements.
     */
    public boolean isEmpty() {
        return false;
    }

    /**
     * Returns a string representation of this list. The string
     * representation consists of a list of the elements enclosed in
     * square brackets ("[]"). Adjacent elements are separated by the
     * characters ", " (comma and space). Elements are converted to
     * strings by the method toString() inherited from Object.
     */
    public String toString() {
        return null;
    }
}
问题是,当我尝试编译时,它会生成一个“找不到符号 - 方法添加(T)”错误消息。我不明白为什么它不起作用,但我是Java的新手,所以我想要一些帮助。

1 个答案:

答案 0 :(得分:0)

是的,LinkedList 有 add 方法,但问题是您没有导入 java.util.LinkedList。您正在实现自己的 LinkedList。因此,要么为您的类实现 add 方法,要么无需实现即可轻松使用 import java.util.LinkedList