2016-04-17 2 views
0

私はJavaでドロップアウトスタックを実装しようとしていますが、現在私のお金のために実行されています!ハハJava初心者:Javaでアレイのドロップアウトスタックを実装する簡単な方法はありますか?

私はこれまでのところ、私が知る限り、私のロジックは健全ですが、コンパイルしていません。私はjava.lang.ArrayIndexOutOfBoundsExceptionを取得し続けています...

私は何をしようとしているのですか?ここではスタックの要素のシリーズをプッシュしてポップしています。新しい要素がスタックの先頭に追加されたときに削除されます。助言がありますか?

マイコード:

import java.util.Arrays; 

public class Base_A05Q2 
{ 
/** 
* Program entry point for drop-out stack testing. 
* @param args Argument list. 
*/  
public static void main(String[] args) 
{ 
    ArrayDropOutStack<Integer> stack = new ArrayDropOutStack<Integer>(4); 

    System.out.println("DROP-OUT STACK TESTING"); 

    stack.push(1); 
    stack.push(2); 
    stack.push(3); 
    stack.push(4);  
    stack.push(5);    

    System.out.println("The size of the stack is: " + stack.size());   
    if(!stack.isEmpty())    
     System.out.println("The stack contains:\n" + stack.toString()); 

    stack.pop();   
    stack.push(7); 
    stack.push(8);  

    System.out.println("The size of the stack is: " + stack.size());     
    if(!stack.isEmpty())    
     System.out.println("The stack contains:\n" + stack.toString()); 
} 

public static class ArrayDropOutStack<T> implements StackADT<T> 
{ 
    private final static int DEFAULT_CAPACITY = 100; 

    private int top; 
    private int bottomElem = 0; 
    private T[] stack; 

    /** 
    * Creates an empty stack using the default capacity. 
    */ 
    public ArrayDropOutStack() 
    { 
     this(DEFAULT_CAPACITY); 
    } 

    /** 
    * Creates an empty stack using the specified capacity. 
    * @param initialCapacity the initial size of the array 
    */ 
    @SuppressWarnings("unchecked") 
    public ArrayDropOutStack(int initialCapacity) 
    { 
     top = -1; 
     stack = (T[])(new Object[initialCapacity]); 
    } 

    /** 
    * Adds the specified element to the top of this stack, expanding 
    * the capacity of the array if necessary. 
    * @param element generic element to be pushed onto stack 
    */ 
    public void push(T element) 
    { 
     if (size() == stack.length) 
      top = 0; 

     stack[top] = element; 
     top++; 
    } 

    /** 
    * Removes the element at the top of this stack and returns a 
    * reference to it. 
    * @return element removed from top of stack 
    * @throws EmptyCollectionException if stack is empty 
    */ 
    public T pop() throws EmptyCollectionException 
    { 
     if (isEmpty()) 
      throw new EmptyCollectionException("stack"); 

     T result = stack[top]; 
     stack[top] = null; 

     if (top == 0) 
      top = size()-1; 
     top--; 

     return result; 
    } 

    /** 
    * Returns a reference to the element at the top of this stack. 
    * The element is not removed from the stack. 
    * @return element on top of stack 
    * @throws EmptyCollectionException if stack is empty 
    */ 
    public T peek() throws EmptyCollectionException 
    { 
     if (isEmpty()) 
      throw new EmptyCollectionException("stack"); 

     return stack[top]; 
    } 

    /** 
    * Returns true if this stack is empty and false otherwise. 
    * @return true if this stack is empty 
    */ 
    public boolean isEmpty() 
    { 
     if(stack.length == 0) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 

    /** 
    * Returns the number of elements in this stack. 
    * @return the number of elements in the stack 
    */ 
    public int size() 
    { 
     int counter = 0; 
     for (int i = 0; i < stack.length; i++) 
     { 
      if (stack[i] != null) 
      { 
       //counter ++; 
      } 
     } 
     return counter; 
    } 

    /** 
    * Returns a string representation of this stack. The string has the 
    * form of each element printed on its own line, with the top most 
    * element displayed first, and the bottom most element displayed last. 
    * If the list is empty, returns the word "empty". 
    * @return a string representation of the stack 
    */ 
    public String toString() 
     { 
      String result = ""; 
      for (int scan = top-1; scan >= 0; scan--) 
      result = result + stack[scan].toString() + "\n"; 
      return result; 
     } 
    } 
} 

私はこの問題は、このブロックであると思いますが、私は問題を突き止めることができません。どんな助けでも大歓迎です!

プッシュ/ポップ操作で一般
public void push(T element) 
    { 
     if (size() == stack.length) 
      top = 0; 

     stack[top] = element; 
     top++; 
    } 
+0

私はあなたが何をしようとしているのか分かりません。 "...新しい要素がスタックの最上部に追加されたときに下の要素がドロップアウトされるようにしたい"というのは、1つの要素スタックのように聞こえる。それがいつ中止するかは何ですか?あなたのコメントは、必要に応じて配列のサイズを変更すると言います。 – ChiefTwoPencils

+0

私がここに持っているコードのいくつかのブロックは、別のスタックを作るために作った(成功した)別の試みから引っ張られました。私はそれが必要かどうかわからないことに多くのコードを掲載しました。それも私の問題の一部かもしれませんが、何を残す必要があるのか​​、何を取り除く必要があるのか​​は分かりません。しかし、あなたの最初の質問に答えるために、私は動的な配列を持つことを試みています(もちろん、配列のサイズは静的ですが、最初の配列に到達すると古い配列のデータを新しい大きな配列にコピーする必要があります容量。私は5つの要素を持つスタックを初期化したい、 –

+0

新しい要素を上に追加し、最も下の要素をポップする –

答えて

1

あなたは何をしようとしていますが、それを認識していないかもしれませんが、円形配列に裏打ちされた固定サイズのスタックを提供しています。

スタッキングを開始するときtop = 0。次に、容量に達するまで十分に挿入し、スタックに「最も古い」値をダンプし、「より新鮮な」データのためのスペースを確保することを選択します。よく、Oth要素が最も古いので、index = sizeにすると、1つの石で2つのバーディを殺すことができませんでした0

は考えてみましょう:

public class CircularStack { 

    int size = 5; 
    int[] arr = new int[size]; 
    int top = 0; 

    public void push(int i) { 
     arr[top++ % size] = i; 
    } 

    public int pop() { 
     return arr[--top % size]; 
    } 
} 

このコードの興味深い部分top++ % size--top % sizeです。彼らは両方ともtopが配列の境界の外に出ることができることを扱います。したがって、サイズが5の配列の場合、唯一の可能なインデックスは{ 0, 1, 2, 3, 4 }になります。

このアプローチでは、もう少し邪悪な問題を導入するのに時間がかかりません。必要に応じてそれを発見して解決するために、あなたに任せておきます。

0

、あなたはインクリメント投稿したい(array[i++] = fooのか、「インデックスをインクリメントし、その後、アレイに追加」)押すと、あなたは(デクリメントを事前にしたいポップfoo = array[--i]または「インデックスをデクリメントしてアレイから値を取得する」

プッシュで配列の最後に到達すると、topは4になります。これは配列の有効なインデックスではないため、次のpopは最初にデクリメントしてから、配列から値を取得する必要があります。

関連する問題