2016-04-07 17 views
-1

Ben java yeni. Kendime dili öğretirken bazı testler yapmaya çalışıyorum. Şimdi bağlantılı bir liste uygulamasındayım. Kodu çevrimiçi test örneklerinden aldım. LinkedList ve LinkedListIterator adlı iki dosya vardır. Ben uygulamayı anlıyorum. Ancak, LinkedList sınıfına yöntem eklemek istiyorum. Bir yöntem (getString()), bağlı listedeki tüm dize değişkenlerinin bir birleşimini görüntülemek için kullanılacaktır. Listenin boyutunu görüntülemek için ikinci yöntem getSize() kullanılır. Bağlantılı listenin şimdiki örneğini almamda sorun yaşıyorum, böylece dizeleri ve büyüklükleri yineleyebilir ve alabilirim. Birisi yardım edebilir mi? Yardım gerçekten takdir edilecektir. iki dosya aşağıdaki gibidir:almak LinkedList örneği in java

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 sınıfı:

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); 
} 

Hata I getSize() ait implmentation çalıştığınızda:

Exception in thread "main" java.lang.StackOverflowError 
    at assignment10.LinkedList.size(LinkedList.java:84) 
    at assignment10.LinkedList.size(LinkedList.java:84) 
    at assignment10.LinkedList.size(LinkedList.java:84) 
    at assignment10.LinkedList.size(LinkedList.java:84) 
+0

için her öğeyi eklemek? Yöntemler boş. Mevcut örnek, '' '' '' 'anahtar sözcüğü ile referans alınabilir. Ama bence sorun bu değil. Değerleri tek tek almak için yineleyiciyi kullanın. – f1sh

+0

“LinkedList” içinde sınıf üyeniz olarak 'int size' olabilir ve sonra sırasıyla 'add' veya 'remove' yöntemlerinde artırabilir veya azaltabilirsiniz. Dize gösterimi için bir yineleyici oluşturmanız ve düğümleri biçimlendirmek için tüm düğümler üzerinde yinelemeniz gerekir. Daha iyi uygulama için lütfen Java'nın Bağlantılı Listesine bakın: https://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html –

+0

Neden varolan Bağlantılı Listeyi genişletmiyorsunuz? Bu zaten size getSize() ile sarmak için size size() yöntemine sahiptir ve sadece İçindeki Dizeleri kullanırsanız, toString() yöntemini kolayca geçersiz kılabilirsiniz. – Akceptor

cevap

0

getSize() olabilir

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

    return size; 
} 

Ancak, büyüklüğünü bilmek için her seferinde listeyi tekrarlamak verimli değildir. Daha iyi çözüm, boyutu sınıf değişkeni olarak depolamak ve listeyi değiştiren yöntemlerde artış veya azalmadır.

toString() yöntemin fikir aynıdır

, liste yineleme ve denemiş Ne sonuç dizesi

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

    return sb.toString; 
}