[英]Java newbie: Is there an easier way to implement an array Drop-Out stack in 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++;
}
您正在尝试做的但可能没有意识到的是,它提供了一个由圆形数组支持的固定大小的堆栈。
当您开始堆叠top = 0
。 然后,插入足够多的数据,直到达到容量为止,然后选择转储堆栈中“最旧的”值并为“刷新器”数据腾出空间。 好吧, Oth元素是最古老的元素,所以当index = size
设为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 }
。
您很快就会意识到这种方法会引入另一个邪恶程度较小的问题。 如有必要,我会留给您发现和解决。
通常,在推入/弹出操作中,当您进行推入操作时,您想后递增( array[i++] = foo
或“将其添加到数组中然后增加索引”),而当您弹出时,则要进行预减量( foo = array[--i]
或“递减索引,然后从数组中获取值”)
当您通过推送到达数组的末尾时, top
将为4,这不是数组的有效索引,因此下一个pop必须先递减然后从数组中获取值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.