java - 如何在堆栈上实现函数 isFull() 的异常

标签 java generics stack

import java.util.*;
import java.lang.Iterable; 

public class MyStackArray <Item> implements Iterable<Item> {
private Item I[];
        private int top;
        private int size;
        private final static int DEFAULT_SIZE = 10;

public MyStackArray () {
        this(DEFAULT_SIZE);
}

public MyStackArray (int capacity) {
        size = capacity;
        I = (Item[]) new Object [capacity];
        top = -1;
}

public Item getTop() {
        if (isEmpty())
            return null;
        return I[top];
}

public boolean isEmpty() {
        return (top == -1);
}

public boolean isFull() {
        return (top == I.length - 1);
}

public Item pop() throws EmptyStackException {
        if (isEmpty())
            throw new EmptyStackException ();
        Item item = I[top];
        I[top--] = null;
        if(top> 0 && top== I.length / 4)
            resize(I.length/2);
        return item;
}

public void push(Item item) throws FullStackException {
        if (isFull())
            throw new FullStackException ();
        if (top== I.length - 1)
            resize(2 * I.length);
        I[++top] = item;
}

public int size() {
        return (top+ 1);
}

private void resize (int newCapacity) {
        Item t[] = (Item[]) new Object[newCapacity];
        for (int i = 0; i <= top; i++)
            t[i] = I[i];
        I = t;
}

public Iterator<Item> iterator() {
        return new MyStackArrayIterator();
}

private class MyStackArrayIterator implements Iterator <Item> {
        private int i = top;

        public boolean hasNext() {
            return (i > -1);
        } 

        public Item next() {
            return I[i--];
        }

        public void remove() {
           throw new UnsupportedOperationException();
        }
    }
}

这是使用泛型方法的 Stack 代码。 对于 isEmpty 来说,一切都很顺利,异常(exception)就是工作。

public boolean isFull() {
            return (top == I.length - 1);
}

我应该改变什么才能isFull()和push()异常可以正常工作? 在驱动程序类中,当最大元素为 5 时,我尝试推送 5 个元素。

push("A");push("B");push("C");push("D");push("E");

size() = 5, getTop() = E

然后我又推送一个元素,我的异常表明堆栈已满。

size() = 5, getTop() = E

所以,我把它们全部弹出了。

Size = 0, getTop = null

我推送了 3 个元素,

push("F");push("G");push("H");

但是程序说栈已经满了,而最多有5个元素。我该如何解决这个问题?

最佳答案

当您弹出大部分元素时(当 top== I.length/4 时),您的 pop 方法会将堆栈的容量减半。

您的 push 方法应该在必要时增加容量,但是 isFull() 阻止它这样做(因为与 isFull() 相同的条件 检查 - (top == I.length - 1) - 也用于确定何时应增加容量)。

如果支持增加容量,isFull()是什么意思?容量要么是固定的,在这种情况下您永远不应该更改它,要么它不是固定的,在这种情况下 isFull() 应始终返回 false。

关于java - 如何在堆栈上实现函数 isFull() 的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30732359/

相关文章:

java - 类 StatusCode 扩展 Enum<StatusCode>

generics - 如何根据 Rust 中的泛型选择常量?

c++ - 堆栈列表调试断言失败 C++

java - SWT 应用程序中 com.ibm.icu.text.BreakDictionary.main 中的 ArrayIndexOutOfBoundsException

C#构造函数泛型参数推断

java - 找不到以下类: - ImageButton (Change to android. widget.ImageButton,修复构建路径,编辑XML)

java - 在java fx中使用堆栈和循环绘制树

.net - Powershell/.Net : Get a reference to an object returned by a method

java - 将 Java 1.8 用于 Android 项目

java - 以原子方式更改 c3p0 ComboPooledDataSource 中的凭据