本篇内容主要讲解“Java数组栈怎么实现”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Java数组栈怎么实现”吧!
package com.loongship.second;public class Stack {private int initSize;private Node[] nodes;private int index = 0;/*** 添加节点** @param node*/public void addNode(Node node) {if (nodes.length == initSize) {Node[] target = new Node[initSize = initSize * 2];System.arraycopy(nodes, 0, target, 0, nodes.length);nodes = target;}nodes[index++] = node;}public Node pop() {return nodes[index - 1];}public Node del() {Node returnVal = pop();nodes[index - 1] = null;return returnVal;}public Stack(int initSize) {this.initSize = initSize;nodes = new Node[initSize];}public static void main(String[] args) {Stack stack = new Stack(1);stack.addNode(new Node());stack.addNode(new Node());stack.addNode(new Node());System.out.println(stack);stack.del();System.out.println(stack);}}
到此,相信大家对“Java数组栈怎么实现”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
原创文章,作者:3628473679,如若转载,请注明出处:https://blog.ytso.com/tech/opensource/227747.html