Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use one-dimensional array to simulate the implementation of stack in Java

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article Xiaobian for you to introduce in detail "Java how to use an one-dimensional array to simulate the implementation of the stack", detailed content, clear steps, details handled properly, I hope that this "Java how to use an one-dimensional array to simulate the implementation of the stack" article can help you solve doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.

Train of thought

First in, then out, first solve the problem of stack pressing, then solve the bullet stack and main method

Function

Simulate the stack at any time

Simulate the bullet stack at any time

Prevent exceptions and errors

You can traverse the variables in the "stack" at any time, and the stack frame is clearly visible!

Source code: import java.util.Scanner;public class MoveTest01 {/ / the traversal array of the stack method uses static int i; / / to create the Object [] array as the stack, and limits the "memory upper limit" to 5; static Object [] os = new Object [5] / / create an array and simulate the control of static num [] l = {new A (), new B (), new C (), new D (), new E (), new F ()}; public static void main (String [] args) {int a = 0traversing the Object [] array control boolean c = true;// control loop boolean d = true / / check stack memory usage to prevent exceptions Scanner s = new Scanner (System.in); do {System.out.println ("= ="); System.out.print ("Please select" push stack "," pop stack "or" list the methods pointed to by variables stored in stack memory ". Enter" exit "to end the program! : "); String z = s.next (); / / determine the user input if (z.equals (" stack ")) {/ / prevent the sequence from exceeding the limit if (a > (os.length-1)) {d = false; System.out.println (" stack memory is full! Please press the stack after the stack! ") ;} if (d) {/ / call the num [] array to simulate the stack l [a] .leng (); ajar;} / prevent if (d) from locking d = true } else if (z.equals ("bullet stack") {/ / call the pop method to simulate the bullet stack and initialize the count pop (); a = 0; I = 0 } else if (z.equals ("exit")) {/ / end the do...while loop body c = false;} else if (z.equals ("listing the methods pointed to by variables stored in stack memory")) {int index =-1 / / create stack frame if (os [0] = = null) {System.out.println ("there are no loaded variables in the stack!") ;} for (int k = os.length-1; k >-1; kmurf -) {/ / decide if each attribute in the Object [] array, if it is not equal to null, then output the declaration if (! (os [k] = = null)) {index++ System.out.println ("the methods for stacked variables in stack memory are:" + os [k]) }} / / if the value of the stack frame is not 0, the output result if (! (index = =-1)) {System.out.println (os [index] + "= = stack frame pointing");} while (c) } / / Analog stack public static void Zhan (Object o) {if (I)

< os.length) { //给Object[]数组赋值 os[i] = o; System.out.println("目标:" + os[i] + "的所有变量已压入栈内!"); i++; } } public static void push(String c){ //接收下面类传来的参数并赋值给Zhan() Zhan(c); } public static void pop(){ //检测数组第一位的值是不是空,如果是则输出消息 if(os[0] == null){ System.out.println("栈内没有已装载的变量!无法弹栈!"); } //模拟弹栈 for(int k = (os.length - 1);k >

) {/ / traverses the array, outputting and initializing all values other than null in the array to null if (! (os [k] = = null)) {System.out.println (os [k] + "all variables: memory popped!") ; os [k] = null;} class num {public void leng () {/ / Let the following methods have a common parent class, and when called, output their own name to stack MoveTest01.push (getClass (). GetName ()) }} / / the method that is about to enter the method area, assuming that there are variables (you can also think of these methods as variables directly) Class An extends num {public A () {} class B extends num {public B () {} class C extends num {public C () {} class D extends num {public D () {} class E extends num {public E () {}} class F extends num {public F () {}, this article "how Java uses one-dimensional arrays to simulate stack implementation" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about the articles, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report