英文:
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<E extends Comparable<E>> extends LinkedList implements Iterable
{
public void main(String args[]){
LinkedList<E> list = new LinkedList<E>();
}
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<E> list
which should be a field in your class and delegate calls to this field.
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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论