会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 133950个问题

为什么size数值为0?
package 栈结构;

import java.util.Arrays;
import java.util.EmptyStackException;

public class MyStack<E>{
    private Object[] arr;
    private int stackLength =4;
    private int size;
    private int index =-1;


    public boolean empty(){
        return this.size==0;
    }


    public E pop(){
        if (this.index == -1){
                throw new EmptyStackException();
    }
        this.size--;
        return  (E)this.arr[index--];
    }


    public  E push(E item){
        this.capacity();
        this.arr[++index]=item;
        return item;
    }

    private void capacity(){
        if ( this.arr==null ){
            this.arr=new Object[this.stackLength];
        }
        if (this.size-(this.stackLength-1)>=0 ){
            this.stackLength=this.stackLength+(this.stackLength>>1);
            this.arr= Arrays.copyOf(this.arr,this.stackLength);
        }
    }

    public static void main( String[] args ) {
        MyStack<String> ms = new MyStack<>();
        ms.push("a");
        ms.push("b");
        ms.push("c");
        System.out.println(ms.size);
        System.out.println(ms.pop() );
        System.out.println(ms.pop() );
        System.out.println(ms.pop() );
    }
}

image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/数据结构 26341楼
WEB前端全系列/第二阶段:JavaScript编程模块/面向对象编程 26343楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 26344楼
JAVA 全系列/第六阶段:JavaWeb开发/Servlet技术详解 26345楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 26346楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 26347楼
JAVA 全系列/第八阶段:Linux入门到实战/Linux(旧) 26348楼
Python 全系列/第七阶段:网页编程基础/CSS 样式 26349楼
JAVA 全系列/第一阶段:AI驱动的JAVA编程/JAVA入门和背景知识 26351楼
Python 全系列/第三阶段:Python 网络与并发编程/网络通信 26352楼
JAVA 全系列/第四阶段:数据库与AI协同技术实战/MySQL数据库 26353楼
Python 全系列/ 第十五阶段:自动化操作办公软件、邮件、定时任务等/自动化操作办公软件、邮件、定时任务等 26354楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637