2016-03-19 7 views
0

私は動物のリストを作成するプログラムを作成することになっています。プログラムは名前でソートし、アルファベット順にリストに入れます。しかし、 "Display Animal"ボタンをクリックするたびに何も表示されず、list.size()の問題であることがわかりましたので、テストするために整数10に変更しました。私は、問題はどこから来ているか分からないと私は本当に困惑している、ここに私のコードです:LinkedOrderedListとLinkedListの実装

これは

package animal; 
import exceptions.ElementNotFoundException; 
import exceptions.EmptyCollectionException; 
import java.util.*; 

/** 
* @author Sharon Umute 
* Comp 139 001B 
* @param <T> 
*/ 
public class LinkedList<T> implements ListADT<T> { 

protected int count; 
protected SinglyLinkedNode head, tail; 
protected int modCount; 

/** 
* The LinkedList constructor creates an empty list 
*/ 
public LinkedList(){ 
    count=0; 
    head=tail=null; 
    modCount = 0; 
} 
/** 
* Removes and returns the head element from this list. 
* 
* @return the head element from this list 
*/ 
@Override 
public T removeFirst() throws EmptyCollectionException { 
    if(isEmpty()){ 
     throw new EmptyCollectionException("list"); 
    } 

    T result=(T) head.getElement(); 
    head=head.getNext(); 
    count--; 
    modCount++; 
    return result; 
} 

/** 
* Removes and returns the tail element from this list. 
* 
* @return the tail element from this list 
*/ 
@Override 
public T removeLast() throws EmptyCollectionException { 
    if(isEmpty()){ 
     throw new EmptyCollectionException("list"); 
    } 

    T result=(T) tail.getElement(); 
    tail=tail.getNext(); 
    count--; 
    modCount++; 
    return result; 
} 

/** 
* Removes and returns the specified element from this list. 
* 
* @param targetElement the target element 
* @return the target element 
*/ 
@Override 
public T remove(T targetElement) throws EmptyCollectionException, ElementNotFoundException { 
    if(isEmpty()){ 
     throw new EmptyCollectionException("list"); 
    } 

    boolean found = false; 
    SinglyLinkedNode<T> previous = null; 
    SinglyLinkedNode<T> current = head; 

    while (current != null && !found) 
     if (targetElement.equals(current.getElement())) 
      found = true; 
     else 
     { 
      previous = current; 
      current = current.getNext(); 
     } 

    if (!found) 
     throw new ElementNotFoundException("LinkedList"); 

    if (size() == 1) // only one element in the list 
     head = tail = null; 
    else if (current.equals(head)) // target is at the head 
     head = current.getNext(); 
    else if (current.equals(tail)) // target is at the tail 
    { 
     tail = previous; 
     tail.setNext(null); 
    } 
    else // target is in the middle 
     previous.setNext(current.getNext()); 

    count--; 
    modCount++; 
    return current.getElement(); 


} 

/** 
* Returns a reference to the head element in this list. 
* 
* @return a reference to the head element in this list 
*/ 
@Override 
public T first() throws EmptyCollectionException { 
    if(isEmpty()){ 
     throw new EmptyCollectionException("list"); 
    } 

    T result=(T) head.getElement(); 
    return result; 
} 

/** 
* Returns a reference to the tail element in this list. 
* 
* @return a reference to the tail element in this list 
*/ 
@Override 
public T last() throws EmptyCollectionException { 
    if(isEmpty()){ 
     throw new EmptyCollectionException("list"); 
    } 

    T result=(T) tail.getElement(); 
    return result; 
} 

/** 
* Returns true if this list contains the specified target element. 
* 
* @param target the target that is being sought in the list 
* @return true if the list contains this element 
*/ 
@Override 
public boolean contains(T target) throws EmptyCollectionException { 
    if(isEmpty()){ 
     throw new EmptyCollectionException("list"); 
    } 
    for(T result:this){ 
     if(result.equals(target)){ 
      return true; 
     } 
    } 
    return false; 
} 

/** 
* Returns true if this list contains no elements. 
* 
* @return true if this list contains no elements 
*/ 
@Override 
public boolean isEmpty() { 
    return(head==null); 
} 

/** 
* Returns the number of elements in this list. 
* 
* @return the integer representation of number of elements in this list 
*/ 
@Override 
public int size() { 
    return count; 
} 

/** 
* Returns an iterator for the elements in this list. 
* 
* @return an iterator over the elements in this list 
*/ 
@Override 
public Iterator<T> iterator() { 
    return new LinkedListIterator(); 
} 


private class LinkedListIterator implements Iterator<T> 
    { 
     private int iteratorModCount; // the number of elements in the collection 
     private SinglyLinkedNode<T> current; // the current position 


    /** 
    * Sets up this iterator using the specified items. 
    * 
    * @param collection the collection the iterator will move over 
    * @param size  the integer size of the collection 
    */ 
    public LinkedListIterator() 
    { 
     current = head; 
     iteratorModCount = modCount; 
    } 

    /** 
    * Returns true if this iterator has at least one more element 
    * to deliver in the iteration. 
    * 
    * @return true if this iterator has at least one more element to deliver 
    *   in the iteration 
    * @throws ConcurrentModificationException if the collection has changed 
    *   while the iterator is in use 
    */ 
    public boolean hasNext() throws ConcurrentModificationException 
    { 
     if (iteratorModCount != modCount) 
      throw new ConcurrentModificationException(); 

     return (current != null); 
    } 

    /** 
    * Returns the next element in the iteration. If there are no 
    * more elements in this iteration, a NoSuchElementException is 
    * thrown. 
    * 
    * @return the next element in the iteration 
    * @throws NoSuchElementException if the iterator is empty 
    */ 
    public T next() throws ConcurrentModificationException 
    { 
     if (!hasNext()) 
      throw new NoSuchElementException(); 

     T result = current.getElement(); 
     current = current.getNext(); 
     return result; 
    } 

    /** 
    * The remove operation is not supported. 
    * 
    * @throws UnsupportedOperationException if the remove operation is called 
    */ 
    public void remove() throws UnsupportedOperationException 
    { 
     throw new UnsupportedOperationException(); 
    } 
} 

} 

LinkeListクラスですこれはLinkedOrderedListクラスです:

package animal; 
/** 
* @author Sharon Umute 
* Comp 139 001B 
* @param <T> 
*/ 
public class LinkedOrderedList<T> extends LinkedList<T> implements OrderedListADT<T>{ 


    /** 
    * Creates an empty list. 
    */ 
    public LinkedOrderedList() 
    { 
     super(); 
    } 

    /** 
    * Adds the specified element to this list at the proper location 
    * 
    * @param element the element to be added to this list 
    */ 
    @Override 
    public void add(T element) { 
     SinglyLinkedNode<T> node =new SinglyLinkedNode<>(element); 
     SinglyLinkedNode<T> previous=null; 
     SinglyLinkedNode<T> current=head; 
     Animal animal; 
     Animal animal2; 
     int result; 
     if(isEmpty()){ 
      tail = head = node; 
     }else{ 
      int i=0; 
      while(i<this.size()){ 
       animal=(Animal) current.getElement(); 
       animal2=(Animal) node.getElement(); 
       result=animal.getName().compareTo(animal2.getName()); 
       if((result==-1)&&(current!=head)&&(current!=tail)){ 
        previous=current; 
        current=current.getNext(); 
        i++; 
       }else if((result==1)&&(current!=head)&&(current!=tail)){ 
        previous.setNext(node); 
       }else if((result==0)&&(current!=head)&&(current!=tail)){ 
        i++; 
       } 
       if((current==head)||((size()==1)&&(result==1))){ 
        node.setNext(head); 
        head=node; 
       }else if((current==tail)||((size()==1)&&(result==-1))){ 
        tail.setNext(node); 
        tail=node; 
       } 
      } 
     } 
    } 
} 

これはSimpleGUIと呼ばれるGUIです。actionPerformed以外は何も変更できませんので、それだけが表示されます:

誰かが問題の原因と解決方法を教えてください。私は本当にそれを感謝します。

+1

私は、誰もがこのコードを歩き回りたいとは思わないと思います。デバッガを使用してコードをステップ実行して、何がうまくいかないかを確認することをお勧めします。 –

+0

"私はそれをテストするために整数10に変更しましたが、それは表示されましたが、追加された最初の動物のみが表示されます。あなたは何匹の動物を期待していますか?あなたはリンクリストリスト 'n'の動物を追加し、間違っている場所を見ることができます。あなたの操作が「表示」と表示されているときに「removeFirst」を呼び出す理由がわからない。 –

答えて

0

これが問題の原因であるかどうかはわかりませんが、リストを表示する方法はちょっと狂っています。いずれかのサイズの実装に何か問題があった場合は、追加または削除し、リストを表示しようとするたびにそれをゴミ箱に入れることができます。

より良い方法は、リストイテレータを使用することです。

もっと一般的には、IDEでデバッガを使用する方法を学ぶ必要があります。デバッガの使用は、すべてのプログラマがマスターする必要がある基本的なスキルです。コードをデバッグできないプログラマは、基本的に雇用主にとって役に立たない。

もう1つのことは、単体テストの作成方法です。

+0

ありがとう、私はデバッガを使用する方法を学ぶ必要があります – Drew

関連する問題