You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

数组实现无界栈扩容后第11个元素无法显示的问题排查

问题分析与解决方案

错误根源

你的push方法逻辑存在漏洞:当栈满触发扩容后,没有将当前要添加的元素放入扩容后的栈中。

具体来看:

  • 初始栈容量10,topOfStack初始为-1,push完A-J后,topOfStack变为9(正好是数组最后一个索引)。
  • 当push第11个元素K时,触发topOfStack == theStack.length -1的扩容条件,你完成了数组扩容,但扩容后直接结束了方法,没有执行元素添加操作,导致K根本没被存入栈。
  • 后续push L和M时,栈已经扩容到20,topOfStack还是9,满足原代码的else条件,执行++topOfStack赋值,所以L和M能正常存入。

修正后的代码

只需要修改push方法,移除else分支,扩容后直接执行元素添加:

public class stackCode
{
    private String[] theStack; //the array-based stack  
    private int stackSize = 10; //the size of stack
    private int topOfStack; //top of stack

    public stackCode()
    {//this is building or initializing the stack within the stack 
        theStack = new String[stackSize]; //the elements within the stack 
        topOfStack = -1;
    }
    
    public void push(String elements)
    {
        if(topOfStack == theStack.length - 1)
        {
            int reallocateSize = (theStack.length) * 2;
            String [] newStack = new String[reallocateSize];
            System.arraycopy(theStack, 0, newStack, 0, theStack.length);
            theStack = newStack;
        }
        // 不管是否扩容,都执行元素添加,移除else分支
        theStack[++topOfStack] = elements;
    }

    public String pop()
    {
        if(isEmpty())
        {
            System.out.println("Stack is empty, please fill in with new values.");
            return null;
        }
        return theStack[topOfStack--];
    }

     
    public void peek(int index)
    {
        if(isEmpty())
        {
            System.out.println("Stack is empty. No values to see");
        }
        else{
            System.out.println(theStack[index]);
        }
    }
    public boolean isEmpty()
    {
        return topOfStack == -1; 
    }
    public void print()
    {
        if(isEmpty())
        {
            System.out.println("Stack is empty");
        }
        else {
            for(int i = topOfStack; i >= 0; i--)
            {
                System.out.println(theStack[i]);
            }
        }
    }

    public static void main(String [] args)
    {
        stackCode theFirstStack = new stackCode();
        theFirstStack.push("A");
        theFirstStack.push("B");
        theFirstStack.push("C");
        theFirstStack.push("D");
        theFirstStack.push("E");
        theFirstStack.push("F");
        theFirstStack.push("G");
        theFirstStack.push("H");
        theFirstStack.push("I");
        theFirstStack.push("J");
        theFirstStack.push("K");
        theFirstStack.push("L");
        theFirstStack.push("M");
        theFirstStack.print();
    }
}

关于topOfStack初始值的说明

将topOfStack初始化为-1是完全正确的栈实现逻辑:

  • -1明确表示栈为空,没有任何元素。
  • 每次push时,先执行++topOfStack,将指针移动到下一个可用的空索引(第一次push时从-1变为0,对应数组第一个位置),再赋值元素,完美匹配数组的索引规则。
  • 你之前改成0能显示K,是因为改变了栈的计数逻辑,但这会导致isEmpty判断(topOfStack == -1)失效,属于错误的修复方式。

内容的提问来源于stack exchange,提问作者Shan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 17:56:04