java基礎常見問題解析(四)堆、常量池、方法區棧與佇列
堆、常量池、方法區棧與佇列
在Java 中,下面關於String 類和StringBuffer 類的描述正確的是哪一個
a. StringBuffer 類的物件呼叫toString()方法將轉化為String 型別
b. 兩個類都有append()方法
c. 可以直接將字串”test” 賦值給宣告的String 類和StringBuffer 類的變數
d. 兩個類的例項的值都能夠改變public void demo01() { StringBuffer bf = new StringBuffer("AAA"); //不可以bf = "AAA" bf.append("BBB"
String物件的建立
“`
// new物件, 與字串直接賦值的區別
public void demo02() {
String s1 = new String(“AAA”);//直接new物件會儲存在記憶體的堆裡邊
String s2 = new String(“AAA”);
// 比較地址
System.out.println(s1 == s2);//false
// 比較物件的值
System.out.println(s1.equals(s2));//true
System.out.println();
// 不用new的字串 會儲存到常量池中
String s3 = “AAA”;
String s4 = “AAA”;
System.out.println(s3 == s4);//true
System.out.println(s3.equals(s4));//true
}
```
![這裡寫圖片描述](https://img-blog.csdn.net/20180512135149972?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE5NzA0MDQ1/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
// 測試StringBuffer與Stirng的不同
public void demo03() {
String s5 = "AAA";
String s6 = s5;
s6 = s6 + "BBB";
// 字串是不可變的,每次的修改本質上都是建立了一個副本,在副本基礎上進行處理和操作
System.out.println(s5 == s6);//false
System.out.println(s5.equals(s6));//false
System.out.println();
// 如果遇到字串的頻繁相加如何處理,StringBuffer來處理,StringBuffer是可變的
StringBuffer s7 = new StringBuffer("AAA");
StringBuffer s8 = s7;
s8.append("BBB");
System.out.println(s7 == s8);//true
System.out.println(s7.equals(s8));//true
}
- 下列”棧的概念”敘述中正確的是
a. 在棧中, 棧中元素隨棧底指標與棧頂指標的變化而動態變化
b. 在棧中, 棧頂指標不變, 棧中的元素對峙棧底指標的變化而動態變化
c. 在棧中, 棧底指標不變, 棧中的元素隨棧頂指標的變化而動態變化
d. 上述三種說法都不對
棧和佇列
棧:後進先出的結構
佇列:先進先出的結構
方法的呼叫的過程就是入棧和出棧的過程
訊息機制和執行緒的執行 –佇列
自定義棧
// 採用陣列自定義棧結構
public class MyStack<T> {
// 設定棧的初始大小
private final int DEFAULT_SIZE = 3;
// 當前棧中的元素
private int size = 0;
// 每次需要的擴充套件大小
private int capacity = 0;
// 永遠指向能夠新增元素的位置,預設是0
private int index = 0;
// 宣告用來儲存資料的容器(陣列)
private Object[] array;
public static void main(String[] args) {
MyStack<String> stack=new MyStack<String>();
System.out.println("判斷陣列是否為空:" + stack.isEmpty()); // true
// 壓棧操作
stack.push("A");
stack.push("B");
stack.push("C");
System.out.println("判斷陣列的元素數量:" + stack.size); // 3
System.out.println("檢視棧頂的元素:" + stack.peek()); // C
System.out.println("判斷陣列的元素數量:" + stack.size); // 3
stack.push("D"); // System.out.println("enlarge..........");
System.out.println("判斷陣列的元素數量:" + stack.size); // 4
System.out.println("出棧操作:" + stack.pop()); // D
System.out.println("出棧操作:" + stack.pop()); // C
System.out.println("出棧操作:" + stack.pop()); // B
System.out.println("出棧操作:" + stack.pop()); // A
System.out.println("判斷陣列是否為空:" + stack.isEmpty()); // true
stack.clear();
System.out.println(stack.size + "," + stack.capacity); // 0 + "," + DEFAULT_SIZE
}
public MyStack() {
this.capacity = this.DEFAULT_SIZE;
this.array = new Object[this.capacity];
}
// 在建立棧的時候自定義大小
public MyStack(int capacity) {
this.capacity = capacity;
this.array = new Object[this.capacity];
}
// 判斷棧是否為空
public boolean isEmpty() {
return size == 0;
}
// 檢視棧頂的元素(元素並不出棧)
@SuppressWarnings("unchecked")
public T peek() {
return (T) this.array[this.index - 1];
}
// 出棧(元素會被刪除)
@SuppressWarnings("unchecked")
public T pop() {
index--;
T t = (T) this.array[this.index];
this.array[index] = null;
this.size--;
return t;
}
// 壓棧操作(如果棧滿,則需要擴充套件棧空間在新增元素)
public void push(T t) {
// 首先判斷棧是否已滿
if (this.size < this.capacity) {
this.array[index] = t;
// 永遠指向能夠新增元素的位置
index++;
this.size++;
} else {
// 擴充容量,然後在新增資料
enlarge();
push(t);
}
}
// 首先建立一個新陣列,然後把原始資料複製到新陣列中. 然後在賦回來
public void enlarge() {
System.out.println("enlarge..........");
// 設定擴充套件的大小
this.capacity = this.capacity + this.DEFAULT_SIZE;
Object[] temp = new Object[capacity];
// 把原始陣列的資料賦值給目標陣列
System.arraycopy(this.array, 0, temp, 0, array.length);
// 把原始陣列的資料清空
Arrays.fill(array, null);
// 吧新的目標陣列,賦值給原始陣列
this.array = temp;
}
// 清空陣列,讓陣列回到原始狀態
public void clear() {
Arrays.fill(array, null);
this.index = this.size = 0;
this.capacity = this.DEFAULT_SIZE;
// 重新建立一個數組物件
this.array = new Object[this.CAPACITY];
}
}
測試類:
public static void main(String[] args) {
Stack<Object> stack = new Stack<Object>(); // 建立堆疊物件
stack.push(new Integer(1)); // 向棧中壓入
stack.push("2"); // 向 棧中 壓入
stack.push(new Double(3.00)); // 向 棧中 壓入
printStack(stack); // 顯示棧中的所有元素
System.out.println("3.00 的位置" + stack.search(3.00));
System.out.println("元素" + stack.pop() + "出棧");
printStack(stack); // 顯示棧中的所有元素
System.out.println("元素" + stack.pop() + "出棧");
printStack(stack); // 顯示棧中的所有元素
System.out.println("元素" + stack.pop() + "出棧");
printStack(stack); // 顯示棧中的所有元素
}
private static void printStack(Stack<Object> stack) {
if (stack.empty())
System.out.println("堆疊是空的,沒有元素");
else {
System.out.print("堆疊中的元素:");
Enumeration<Object> items = stack.elements(); // 得到 stack 中的列舉物件
while (items.hasMoreElements())
// 顯示列舉(stack ) 中的所有元素
System.out.print(items.nextElement() + " ");
}
System.out.println(); // 換行
}
}