私は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++;
}
私はあなたが何をしようとしているのか分かりません。 "...新しい要素がスタックの最上部に追加されたときに下の要素がドロップアウトされるようにしたい"というのは、1つの要素スタックのように聞こえる。それがいつ中止するかは何ですか?あなたのコメントは、必要に応じて配列のサイズを変更すると言います。 – ChiefTwoPencils
私がここに持っているコードのいくつかのブロックは、別のスタックを作るために作った(成功した)別の試みから引っ張られました。私はそれが必要かどうかわからないことに多くのコードを掲載しました。それも私の問題の一部かもしれませんが、何を残す必要があるのか、何を取り除く必要があるのかは分かりません。しかし、あなたの最初の質問に答えるために、私は動的な配列を持つことを試みています(もちろん、配列のサイズは静的ですが、最初の配列に到達すると古い配列のデータを新しい大きな配列にコピーする必要があります容量。私は5つの要素を持つスタックを初期化したい、 –
新しい要素を上に追加し、最も下の要素をポップする –