Java Stack

  • 根据这里的介绍

    实现了一个用Java写的Stack

    推荐使用alibaba出的IDEA插件p3c

    对程序风格规范提出了很多很友好的建议

    适合新手学Java时规范自己的写法

    IDEA插件市场搜索alibaba

    显示的第一个Alibaba Java Coding Guidelines就是p3c(不知道为什么这么叫

  • Main.java

    package main;
    
    import module.MyStack;
    
    import java.util.Date;
    
    /**
     * @author FakeCoder
     * @date Tue Feb 11 20:20:37 CST 2020
     */
    public class Main {
        public static void main(String[] args) {
            MyStack stack = new MyStack(10);
            stack.push(1);
            stack.push(2);
            int size = stack.getSize();
            System.out.println(stack.isEmpty());
            for (int i = 0; i < size; i++) {
                System.out.println(stack.pop());
            }
            System.out.println(stack.isEmpty());
        }
    }
  • MyStack.java

    package module;
    
    import java.util.Arrays;
    
    /**
     * @author FakeCoder
     */
    public class MyStack {
        private int[] storage;
        private int capacity;
        private int count;
    
        /**
         * default init function
         */
        public MyStack() {
            this.storage = new int[10];
            this.capacity = 10;
            this.count = 0;
        }
    
        /**
         * init function with initial capacity size
         * @param initCapacity initial capacity
         */
        public MyStack(int initCapacity) {
            if (initCapacity < 1) {
                throw new IllegalArgumentException("initCapacity too small...");
            }
            this.storage = new int[initCapacity];
            this.capacity = initCapacity;
            this.count = 0;
        }
    
        /**
         * push a number into stack
         * @param num number to be pushed in
         */
        public void push(int num) {
            if (this.count == this.capacity) {
                this.addCapacity();
            }
            this.storage[count++] = num;
        }
    
        /**
         * pop a number out of this stack
         */
        public int pop() {
            this.count--;
            if (this.count == -1) {
                this.count++;
                throw new IllegalArgumentException("stack is empty");
            }
            return this.storage[this.count];
        }
    
        /**
         * return the number on stack top
         * @return stack top number
         */
        public int peek() {
            if (this.count == 0) {
                throw new IllegalArgumentException("stack is empty");
            } else {
                return this.storage[this.count--];
            }
        }
    
        /**
         * add capacity by 10 each time
         */
        public void addCapacity() {
            this.storage = Arrays.copyOf(storage, 10);
            this.capacity = this.capacity + 10;
        }
    
        /**
         * return a bool value indicates whether stack is empty or not
         * @return Boolean
         */
        public Boolean isEmpty() {
            return count == 0;
        }
    
        /**
         * return stack's size
         * @return size
         */
        public int getSize() {
            return this.count;
        }
    }

评论
 Current
Java Stack Java Stack
根据这里的介绍 实现了一个用Java写的Stack 推荐使用alibaba出的IDEA插件p3c 对程序风格规范提出了很多很友好的建议 适合新手学Java时规范自己的写法 IDEA插件市场搜索alibaba 显示的第一个Alibaba J
2020-02-11 IcedOtaku
Next 
Latex with VSCode Latex with VSCode
很多Latex的发行版自带的编译器界面风格十分老旧 甚至之前装CTex还被清空了系统环境变量…一言难尽… 现在借鉴TripleZ的博客搭建了VS code上面的编辑环境 配合NUAATUG开源的南航本科、硕士、博士学位论文 LaTeX 模板
2020-02-10 IcedOtaku