Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取java中的LinkedList实例_Java_Linked List_Listiterator - Fatal编程技术网

获取java中的LinkedList实例

获取java中的LinkedList实例,java,linked-list,listiterator,Java,Linked List,Listiterator,我是java新手。在自学语言的同时,我一直在尝试做一些测试。现在我正在使用链表实现。我把测试样本中的代码扔到了网上。有两个文件,LinkedList和LinkedListIterator。我很好地理解了实施过程。但是,我想向LinkedList类添加方法。一个方法(getString())将用于显示linkedlist中所有字符串变量的串联。第二个方法getSize()将用于显示列表的大小。我很难获取linkedlist的当前实例,因此我可以迭代并获取字符串和大小。有人能帮忙吗?我会非常感激你的

我是java新手。在自学语言的同时,我一直在尝试做一些测试。现在我正在使用链表实现。我把测试样本中的代码扔到了网上。有两个文件,LinkedList和LinkedListIterator。我很好地理解了实施过程。但是,我想向LinkedList类添加方法。一个方法(getString())将用于显示linkedlist中所有字符串变量的串联。第二个方法getSize()将用于显示列表的大小。我很难获取linkedlist的当前实例,因此我可以迭代并获取字符串和大小。有人能帮忙吗?我会非常感激你的帮助。 这两个文件如下:

import java.util.NoSuchElementException;

public class LinkedList
{
   //nested class to represent a node
   private class Node
   {
          public Object data;
          public Node next;
   }

   //only instance variable that points to the first node.
   private Node first;

   // Constructs an empty linked list.
   public LinkedList()
   {
      first = null;
   }


   // Returns the first element in the linked list.
   public Object getFirst()
   {
      if (first == null)
       {
         NoSuchElementException ex
             = new NoSuchElementException();
         throw ex;
       }
      else
         return first.data;
   }

   // Removes the first element in the linked list.
   public Object removeFirst()
   {
      if (first == null)
       {
         NoSuchElementException ex = new NoSuchElementException();
         throw ex;
       }
      else
       {
         Object element = first.data;
         first = first.next;  //change the reference since it's removed.
         return element;
       }
   }

   // Adds an element to the front of the linked list.
   public void addFirst(Object element)
   {
      //create a new node
      Node newNode = new Node();
      newNode.data = element;
      newNode.next = first;
      //change the first reference to the new node.
      first = newNode;
   }

   // Returns an iterator for iterating through this list.
   public ListIterator listIterator()
   {
      return new LinkedListIterator();
   }


   public String toString(){

      }

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

   //nested class to define its iterator
   private class LinkedListIterator implements ListIterator
   {
      private Node position; //current position
      private Node previous; //it is used for remove() method

      // Constructs an iterator that points to the front
      // of the linked list.

      public LinkedListIterator()
      {
         position = null;
         previous = null;
      }

     // Tests if there is an element after the iterator position.
     public boolean hasNext()
      {
         if (position == null) //not traversed yet
          {
             if (first != null)
                return true;
             else
                return false;
          }
         else
           {
              if (position.next != null)
                 return true;
              else
                 return false;
           }
      }

      // Moves the iterator past the next element, and returns
      // the traversed element's data.
      public Object next()
      {
         if (!hasNext())
          {
           NoSuchElementException ex = new NoSuchElementException();
           throw ex;
          }
         else
          {
            previous = position; // Remember for remove

            if (position == null)
               position = first;
            else
               position = position.next;

            return position.data;
          }
      }

      // Adds an element before the iterator position
      // and moves the iterator past the inserted element.
      public void add(Object element)
      {
         if (position == null) //never traversed yet
         {
            addFirst(element);
            position = first;
         }
         else
         {
            //making a new node to add
            Node newNode = new Node();
            newNode.data = element;
            newNode.next = position.next;
            //change the link to insert the new node
            position.next = newNode;
            //move the position forward to the new node
            position = newNode;
         }
         //this means that we cannot call remove() right after add()
         previous = position;
      }

      // Removes the last traversed element. This method may
      // only be called after a call to the next() method.
      public void remove()
      {
         if (previous == position)  //not after next() is called
          {
            IllegalStateException ex = new IllegalStateException();
            throw ex;
          }
         else
          {
           if (position == first)
            {
              removeFirst();
            }
           else
            {
              previous.next = position.next; //removing
            }
           //stepping back
           //this also means that remove() cannot be called twice in a row.
           position = previous;
      }
      }

      // Sets the last traversed element to a different value.
      public void set(Object element)
      {
         if (position == null)
          {
            NoSuchElementException ex = new NoSuchElementException();
            throw ex;
          }
         else
          position.data = element;
      }

   } //end of LinkedListIterator class
} 
LinkedListIterator类:

public interface ListIterator
{
   //Move Moves the iterator past the next element.
   Object next();

   // Tests if there is an element after the iterator position.
   boolean hasNext();

   // Adds an element before the iterator position
   // and moves the iterator past the inserted element.
   void add(Object element);


   // Removes the last traversed element. This method may
   // only be called after a call to the next() method.
   void remove();

   // Sets the last traversed element to a different value.
   void set(Object element);
}
尝试实现getSize()时出错:

getSize()
可以是

public int getSize(){
    int size = 0;
    ListIterator iterator = listIterator();
    while(iterator.hasNext()) {
        iterator.next();
        size++;
    }

    return size;
}
但每次迭代列表以了解其大小是不高效的。更好的解决方案是将大小存储为类变量,并增加或减少修改列表的方法

toString()
方法的思想是相同的,迭代列表并将每个项附加到结果字符串中

public String toString(){
    StringBuilder sb = new StringBuilder();
    ListIterator iterator = listIterator();
    while(iterator.hasNext()) {
        sb.append(String.valueOf(iterator.next())).append(",");
    }

    return sb.toString;
}

你试过什么?方法是空的。当前实例可以通过关键字
this
引用。但我认为这不是问题所在。使用迭代器逐个获取值。您可以在
LinkedList
中将
int size
作为类成员,然后分别在
add
remove
方法中增加或减少它。对于字符串表示,您必须创建一个迭代器并遍历所有节点以格式化节点。请参考Java的LinkedList以获得更好的实现:为什么不扩展现有的LinkedList呢?它已经有size()方法,所以您可以用getSize()将其包装,而且如果您只使用其中的字符串,您可以很容易地重写toString()方法
public String toString(){
    StringBuilder sb = new StringBuilder();
    ListIterator iterator = listIterator();
    while(iterator.hasNext()) {
        sb.append(String.valueOf(iterator.next())).append(",");
    }

    return sb.toString;
}