LinkedList问题

huangapple go评论70阅读模式
英文:

LinkedList issues

问题

我正在尝试创建一个名为MyLinkedList的类它是一个单向链表非循环列表每个节点只有一个下一个链接列表有一个头部链接和一个尾部链接该类还应实现可迭代接口我遇到的问题是在addFirst和addEnd中我遇到了一个错误错误消息是无法找到符号 - 变量list我不知道为什么会出现这个错误因为这种布局对于ArrayList是有效的

public class MyLinkedList<E extends Comparable<E>> extends LinkedList implements Iterable {
    LinkedList<E> list = new LinkedList<E>();

    public static void main(String args[]) {
        LinkedList<E> list = new LinkedList<E>();
    }

    public Iterator<E> iterator() {
        // 实现迭代器接口的代码
    }

    public void addFirst(E value) {
        list.addFirst(value);
    }

    public void addEnd(E value) {
        list.add(value);
    }
}
英文:

I am trying to create a MyLinkedList class that is a singly linked, non-circular list where each node only has one link next and the list has a head and a tail link. The class is also supposed to implement an iterable interface. I am encountering an issue wherein addFirst and addEnd I get an error of cannot find symbol - variable list. I don't know why this error is occurring as this layout worked for ArrayLists.

 public class MyLinkedList&lt;E extends Comparable&lt;E&gt;&gt; extends LinkedList implements Iterable
    {
        public  void main(String args[]){
            LinkedList&lt;E&gt; list = new LinkedList&lt;E&gt;();
        }
        
        public Iterator iterator(){
            
             
            
        }
        
        public void addFirst(E value){
            list.addFirst(value);
        }
        
        public void addEnd(E value){
            list.add(value);
        }
    }

答案1

得分: 1

似乎您正在尝试为一个LinkedList<E> list实现一个包装器,该包装器应该是您的类中的一个字段,并将调用委托给这个字段。

public class MyLinkedList<E extends Comparable<E>> extends LinkedList implements Iterable {
    private LinkedList<E> list = new LinkedList<>();

    @Override  
    public Iterator iterator() {       
        return list.iterator();       
    }
        
    public void addFirst(E value) {
        list.addFirst(value);
    }
        
    public void addEnd(E value) {
        list.add(value);
    }
}
英文:

It seems that you you try to implement a wrapper for a LinkedList&lt;E&gt; list which should be a field in your class and delegate calls to this field.

public class MyLinkedList&lt;E extends Comparable&lt;E&gt;&gt; extends LinkedList implements Iterable {
    private LinkedList&lt;E&gt; list = new LinkedList&lt;&gt;();

    @Override  
    public Iterator iterator() {       
        return list.iterator();       
    }
        
    public void addFirst(E value) {
        list.addFirst(value);
    }
        
    public void addEnd(E value) {
        list.add(value);
    }
}

huangapple
  • 本文由 发表于 2020年9月28日 02:20:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64091844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定