JAVA学习(小白向)—栈—2021.6.2
Q1:什么是栈?
A1:堆和栈都是Java用来在RAM中存放数据的地方。
一、堆
(1)Java的堆是一个运行时数据区,类的对象从堆中分配空间。这些对象通过new等指令建立,通过垃圾回收器来销毁。
(2)堆的优势是可以动态地分配内存空间,需要多少内存空间不必事先告诉编译器,因为它是在运行时动态分配的。但缺点是,由于需要在运行时动态分配内存,所以存取速度较慢。
二、栈
(1)栈中主要存放一些基本数据类型的变量(byte,short,int,long,float,double,boolean,char)和对象的引用。
(2)栈的优势是,存取速度比堆快,栈数据可以共享。但缺点是,存放在栈中的数据占用多少内存空间需要在编译时确定下来,缺乏灵活性。
(3)函数中定义的基本类型变量,对象的引用变量都在函数的栈内存中分配。 栈内存特点,数数据一执行完毕,变量会立即释放,节约内存空间。 栈内存中的数据,没有默认初始化值,需要手动设置。
Q2:什么是push、pop?
A2:pop()是移除堆栈顶部的元素并且返回它的值
push()是把对象压入堆栈的顶部
代码
package a13;
/**
* ********************
* Char stack. Do not use Stack because it's already defined in Java.
* @author hengyuzuo
* ********************
*/
public class CharStack {
/**
* *************
* The depth
* *************
*/
public static final int MAX_DEPTH = 10;
/**
* *************
* The actual depth
* *************
*/
int depth;
/**
* *************
* The data;
* *************
*/
char[] data;
/**
* *************
* Construct an empty sequential list.
* *************
*/
public CharStack() {
depth = 0;
data = new char[MAX_DEPTH];
}// Of the first constructor
/**
* *************
* Overrides the method claimed in Object, the superclass of any class.
* *************
*/
public String toString() {
String resultString = "";
for (int i = 0; i < depth; i++) {
resultString += data[i];
}// Of for i
return resultString;
}// Of toSting
/**
* *************************
* Push an element.
* @param paraChar the given char.
* @return Success or not.
* *************************
*/
public boolean push(char paraChar) {
if (depth == MAX_DEPTH) {
System.out.println("Stack full.");
return false;
}//Of if
data[depth] = paraChar;
depth++;
return true;
}// Of push
/**
* *************************
* Pop an element.
* @param paraChar the given char.
* @return Success or not.
* *************************
*/
public char pop() {
if (depth == 0) {
System.out.println("Nothin to pop.");
return '\0';
}// Of pop
char resultChar = data[depth -1];
depth--;
return resultChar;
}// Of pop
/**
* **************************
* The entrance of the program.
* @param args not used now.
* **************************
*/
public static void main(String args[]) {
CharStack tempStack = new CharStack();
for (char ch = 'a'; ch < 'm'; ch++) {
tempStack.push(ch);
System.out.println("The current stack is: " + tempStack);
}// Of for i
char tempChar;
for (int i = 0; i < 11; i++) {
tempChar = tempStack.pop();
System.out.println("Poped: " + tempChar);
System.out.println("The current stack is: " +tempStack);
}// Of for i
}// Of for mian
}// Of CharStack
运行结果
The current stack is: a
The current stack is: ab
The current stack is: abc
The current stack is: abcd
The current stack is: abcde
The current stack is: abcdef
The current stack is: abcdefg
The current stack is: abcdefgh
The current stack is: abcdefghi
The current stack is: abcdefghij
Stack full.
The current stack is: abcdefghij
Stack full.
The current stack is: abcdefghij
Poped: j
The current stack is: abcdefghi
Poped: i
The current stack is: abcdefgh
Poped: h
The current stack is: abcdefg
Poped: g
The current stack is: abcdef
Poped: f
The current stack is: abcde
Poped: e
The current stack is: abcd
Poped: d
The current stack is: abc
Poped: c
The current stack is: ab
Poped: b
The current stack is: a
Poped: a
The current stack is:
Nothin to pop.
Poped: