- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 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;
}
}
}
我认为问题出在这个 block 上,但我无法确定问题所在。非常感谢任何帮助!
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 }
。
你很快就会意识到这种方法会带来另一个不那么严重的问题;如果有必要的话,我会把它留给你去发现和解决。
关于Java新手: Is there an easier way to implement an array Drop-Out stack in Java?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36682871/
我经常在 C 标准文档中看到“实现定义”的说法,并且非常将其作为答案。 然后我在 C99 标准中搜索它,并且: ISO/IEC 9899/1999 (C99) 中第 §3.12 条规定: 3.12 I
“依赖于实现”中的“实现”是什么意思? “依赖于实现”和“依赖于机器”之间有什么区别? 我使用C,所以你可以用C解释它。 最佳答案 当 C 标准讨论实现时,它指的是 C 语言的实现。因此,C 的实现就
我刚刚在 Android-studio 中导入了我的项目,并试图在其中创建一个新的 Activity。但我无法在 android-studio 中创建 Activity 。我指的是here我看不到将目
我想知道您对为什么会发生此错误的意见。在陆上生产环境中,我们使用 CDH4。在我们的本地测试环境中,我们只使用 Apache Hadoop v2.2.0。当我运行在 CDH4 上编译的同一个 jar
我正在尝试集成第三方 SDK (DeepAR)。但是当我构建它时,它会显示一个错误。我试图修复它。如果我创建一个简单的新项目,它就可以正常工作。但是我现有的应用程序我使用相机和 ndk。请帮我找出错误
我很好奇为什么我们有 @Overrides 注释,但接口(interface)没有类似的习惯用法(例如 @Implements 或 @Implementation)。这似乎是一个有用的功能,因为您可能
我对 DAODatabase(适用于 Oracle 11 xe)的 CRUD 方法的实现感到困惑。问题是,在通常存储到 Map 集合的情况下,“U”方法(更新)会插入新元素或更新它(像 ID:Abst
Java-API 告诉我特定类实现了哪些接口(interface)。但有两种不同类型的信息,我不太确定这意味着什么。例如,对于“TreeSet”类:https://docs.oracle.com/en
我有一个接口(interface) MLService,它具有与机器学习算法的训练和交叉验证相关的基本方法,我必须添加两个接口(interface)分类和预测,它们将实现 MLService 并包含根
我一直想知道如何最好地为所有实现相同接口(interface)的类系列实现 equals()(并且客户端应该只使用所述接口(interface)并且永远不知道实现类)。 我还没有编写自己的具体示例,但
我有一个接口(interface)及其 2 个或更多实现, public interface IProcessor { default void method1() { //logic
我有同一个应用程序的免费版和高级版(几乎相同的代码,相同的类,到处都是“if”, list 中的不同包, list 中的进程名称相同)。主要 Activity 使用 IMPLICIT Intent 调
这是我为我的应用程序中的错误部分编写的代码 - (id)initWithData:(NSData *)data <-------- options:(NSUInteger)opti
请查找随附的代码片段。我正在使用此代码将文件从 hdfs 下载到我的本地文件系统 - Configuration conf = new Configuration(); FileSys
我想在 MongoDB 中使用 Grails2.5 中的“ElasticSearch”插件。我的“BuildConfig.groovy”文件是: grails.servlet.version = "3
我收到一条错误消息: fatal error: init(coder:) has not been implemented 对于我的自定义 UITableViewCell。该单元格未注册,在 Stor
得到这个错误 kotlin.NotImplementedError: An operation is not implemented: not implemented 我正在实现一个 ImageBut
typedef int Element; typedef struct { Element *stack; int max_size; int top; } Stack; //
Playground 代码 here 例子: interface IFoo { bar: number; foo?: () => void; } abstract class Abst
我想知道如何抑制警告: Category is implementing a method which will also be implemented by its primary class. 我
我是一名优秀的程序员,十分优秀!