How to implement Java Array Stack
This article mainly explains "how to implement Java array stack". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to implement the Java array stack.
Package com.loongship.second
Public class Stack {private int initSize; private Node [] nodes; private int index = 0
/ * add nodes * * @ 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 ()); System.out.println (stack); stack.del (); System.out.println (stack);} so far, I believe you have a deeper understanding of "how to implement Java array stack", so you might as well do it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!