通用堆栈实现

MAA*_*MAA 4 java generics stack

我正在尝试实现通用堆栈。

这是界面

package stack;

public interface Stack<T>{
    void push(T number);
    T pop();
    T peek();
    boolean isEmpty();
    boolean isFull();
}
Run Code Online (Sandbox Code Playgroud)

这是课程

package stack;

import java.lang.reflect.Array;
import java.util.EmptyStackException;

public class StackArray <T> implements Stack<T>{
    private int maxSize;
    private T[] array;
    private int top;

    public StackArray(int maxSize) {
        this.maxSize = maxSize;
//        @SuppressWarnings("unchecked")
        this.array = (T[]) Array.newInstance(StackArray.class, maxSize);
        this.top = -1;
    }

    private T[] resizeArray() {
        /**
         * create a new array double the size of the old, copy the old elements then return the new array */
        int newSize = maxSize * 2;
        T[] newArray = (T[]) Array.newInstance(StackArray.class, newSize);
        for(int i = 0; i < maxSize; i++) {
            newArray[i] = this.array[i];
        }
        return newArray;
    }

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

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

    public void push(T element) {
        if(!this.isFull()) {
            ++top;
            array[top] = element;
        }
        else {
            this.array = resizeArray();
            array[++top] = element;
        }
    }

    public T pop() {
        if(!this.isEmpty())
            return array[top--];
        else {
            throw new EmptyStackException();
        }
    }

    public T peek() {
        return array[top];
    }
}
Run Code Online (Sandbox Code Playgroud)

这里的主要类

package stack;


public class Main {
    public static void main(String[] args) {
        String word = "Hello World!";
        Stack <Character>stack = new StackArray<>(word.length());

//        for(Character ch : word.toCharArray()) {
//            stack.push(ch);
//        }

        for(int i = 0; i < word.length(); i++) {
            stack.push(word.toCharArray()[i]);
        }

        String reversedWord = "";
        while(!stack.isEmpty()) {
            char ch = (char) stack.pop();
            reversedWord += ch;
        }
        System.out.println(reversedWord);

    }
}
Run Code Online (Sandbox Code Playgroud)

错误是

Exception in thread "main" java.lang.ArrayStoreException: java.lang.Character
    at stack.StackArray.push(StackArray.java:40)
    at stack.Main.main(Main.java:14)
Run Code Online (Sandbox Code Playgroud)

线40处于推法

        array[top] = element;
Run Code Online (Sandbox Code Playgroud)

一边问:什么办法来抑制构造函数中的警告?:)

msa*_*ord 7

根本问题是类型擦除。这样做的相关含义意味着Stack该类的实例在运行时不知道其类型参数。这就是为什么你不能只用最自然的解决方案在这里的原因,array = new T[maxSize]

你已经尝试解决此通过创建一个数组Array.newInstance(...),但不幸的是这个数组没有类型的元素T无论是。在所示的代码中,元素的类型为StackArray,这可能不是您想要的。

处理这个问题的常用方法是使用数组Object内部来Stack,扔任何返回值类型T的访问方法。

class StackArray<T> implements Stack<T> {
    private int maxSize;
    private Object[] array;
    private int top;

    public StackArray(int maxSize) {
        this.maxSize = maxSize;
        this.array = new Object[maxSize];
        this.top = -1;
    }

    // ... lines removed ...

    public T pop() {
        if(this.isEmpty())
            throw new EmptyStackException();
        return element(top--);
    }

    public T peek() {
        if(this.isEmpty())
            throw new EmptyStackException();
        return element(top);
    }

    // Safe because push(T) is type checked.
    @SuppressWarnings("unchecked")
    private T element(int index) {
        return (T)array[index];
    }
}
Run Code Online (Sandbox Code Playgroud)

还要注意,您的resizeArray()方法中有一个错误,maxSize从未分配新值。您真的不需要跟踪maxSize,只需使用即可array.length

我认为peek()原始代码中的堆栈为空时也存在问题。