LinkedList问题

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

LinkedList issues

问题

  1. 我正在尝试创建一个名为MyLinkedList的类它是一个单向链表非循环列表每个节点只有一个下一个链接列表有一个头部链接和一个尾部链接该类还应实现可迭代接口我遇到的问题是在addFirstaddEnd我遇到了一个错误错误消息是无法找到符号 - 变量list我不知道为什么会出现这个错误因为这种布局对于ArrayList是有效的
  2. public class MyLinkedList<E extends Comparable<E>> extends LinkedList implements Iterable {
  3. LinkedList<E> list = new LinkedList<E>();
  4. public static void main(String args[]) {
  5. LinkedList<E> list = new LinkedList<E>();
  6. }
  7. public Iterator<E> iterator() {
  8. // 实现迭代器接口的代码
  9. }
  10. public void addFirst(E value) {
  11. list.addFirst(value);
  12. }
  13. public void addEnd(E value) {
  14. list.add(value);
  15. }
  16. }
英文:

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.

  1. public class MyLinkedList&lt;E extends Comparable&lt;E&gt;&gt; extends LinkedList implements Iterable
  2. {
  3. public void main(String args[]){
  4. LinkedList&lt;E&gt; list = new LinkedList&lt;E&gt;();
  5. }
  6. public Iterator iterator(){
  7. }
  8. public void addFirst(E value){
  9. list.addFirst(value);
  10. }
  11. public void addEnd(E value){
  12. list.add(value);
  13. }
  14. }

答案1

得分: 1

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

  1. public class MyLinkedList<E extends Comparable<E>> extends LinkedList implements Iterable {
  2. private LinkedList<E> list = new LinkedList<>();
  3. @Override
  4. public Iterator iterator() {
  5. return list.iterator();
  6. }
  7. public void addFirst(E value) {
  8. list.addFirst(value);
  9. }
  10. public void addEnd(E value) {
  11. list.add(value);
  12. }
  13. }
英文:

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.

  1. public class MyLinkedList&lt;E extends Comparable&lt;E&gt;&gt; extends LinkedList implements Iterable {
  2. private LinkedList&lt;E&gt; list = new LinkedList&lt;&gt;();
  3. @Override
  4. public Iterator iterator() {
  5. return list.iterator();
  6. }
  7. public void addFirst(E value) {
  8. list.addFirst(value);
  9. }
  10. public void addEnd(E value) {
  11. list.add(value);
  12. }
  13. }

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:

确定