Java程式統計棧中所有元素


在本教程中,我們將學習如何使用各種方法來統計棧中元素的數量。在Java中,棧是一種遵循後進先出 (LIFO) 原則的基本資料結構,這意味著最近新增到棧中的元素將首先被訪問。

棧的實際應用包括函式呼叫管理、表示式求值等。在這些場景中,我們可能需要統計棧中元素的數量。例如,在使用棧進行函式呼叫管理時統計函式呼叫的總數,以及在使用棧對數學表示式進行求值時統計要執行的操作總數。

在這裡,我們將探討以下三種統計棧中元素數量的方法。

  • 使用Stack.size()方法
  • 使用For迴圈(迭代方法)
  • 使用遞迴方法

使用Stack.size()方法

統計棧中元素數量的第一種方法是使用Stack.size()方法。它可以幫助找到棧的大小,這等同於棧中元素的總數。

語法

使用者可以按照以下語法在Java中使用sack.size()方法。

s1.size();

在上述語法中,“s1”是一個包含數字、字串、布林值等元素的棧資料結構。


引數

Stack.size()方法不接受任何引數。


返回值

Stack.size()方法返回棧中元素的總數。


示例

在下面的程式碼中,我們定義了“s1”棧。之後,我們在棧中插入了3個整數。當我們使用size()方法與棧一起使用時,它返回“3”作為輸出,表示棧中元素的總數。

import java.util.Stack;

public class StackExample {
public static void main(String[] args) {
Stack s1 = new Stack<>();

// Insert elements onto the stack
s1.push(1);
s1.push(2);
s1.push(3);

// Get number of elements using size() method
int count = s1.size();

// Print the count of elements
System.out.println("Number of elements in the stack: " + count);
}
}


輸出


Number of elements in the stack: 3


使用For迴圈(迭代方法)

現在,讓我們看一下統計棧中元素數量的第二種方法。在這種方法中,我們將使用“for”迴圈遍歷棧的每個元素,並統計棧中元素的總數。


語法

使用者可以按照以下語法使用“for”迴圈統計棧中元素的總數。

for (Integer element : s1) {
count++;
}

在上述語法中,“s1”是一個棧,我們正在遍歷“s1”棧的元素。在迴圈體中,我們將“count”變數的值加1,該變數儲存棧中元素的數量。


示例

在下面的示例中,我們使用“for”迴圈遍歷棧的每個元素,並在每次迭代中遞增“count”變數的值。之後,我們列印“count”變數的值,該值是棧中元素的數量。

import java.util.Stack;

public class StackCountIterative {
public static void main(String[] args) {
Stack s1 = new Stack<>();

// Insert elements onto the stack
s1.push(1);
s1.push(2);
s1.push(3);

// Count elements using iteration
int count = 0;
for (Integer element : s1) {
count++;
}

// Print the count of elements
System.out.println("Number of elements in the stack: " + count);
}
}

輸出

Number of elements in the stack: 3


使用遞迴方法

統計所有棧元素的第三種方法是使用遞迴。在這種方法中,我們將遞迴地遍歷棧的每個元素,並跟蹤棧中元素的總數。

語法

使用者可以按照以下語法使用遞迴方法統計所有棧元素。

if (s1.isEmpty()) {
return 0;
}
// Remove the top element and count the rest
Integer element = s1.pop();
int count = 1 + countElements(s1);
// Push the element back to restore the stack
s1.push(element);

在上述語法中,我們遵循以下步驟


  1. 如果棧為空,則返回“0”,表示棧中沒有元素。
  2. 從棧中移除元素,因為我們將在下一步中統計當前元素的出現次數。
  3. 使用更新後的棧進行遞迴呼叫,並將它的結果值加1並存儲在“count”變數中。在這裡,我們為之前移除的元素加1。
  4. 接下來,將“element”再次推入棧中,以保持棧狀態不變。


示例

在這個例子中,我們使用了遞迴方法來計算棧中元素的數量。

import java.util.Stack;

public class StackCountRecursive {
public static void main(String[] args) {
Stack s1 = new Stack<>();

// Push some elements onto the stack
s1.push(1);
s1.push(2);
s1.push(3);

// Count elements using recursion
int count = countElements(s1);

// Print the count of elements
System.out.println("Number of elements in the stack: " + count);
}

// Recursive method to count elements
public static int countElements(Stack s1) {
if (s1.isEmpty()) {
return 0;
}
// Remove the top element and count the rest
Integer element = s1.pop();
int count = 1 + countElements(s1);
// Push the element back to restore the stack
s1.push(element);
return count;
}
}

輸出

Number of elements in the stack: 3

結論

我們探討了三種統計棧中元素總數的方法。第一種方法使用Stack.size()方法,簡單直接。第二種方法使用“for”迴圈統計棧元素,比第一種方法稍微複雜一些。第三種方法使用遞迴統計棧元素,對於初學者來說可能比較複雜。


如果您需要在統計棧元素的同時對每個元素執行一些操作,則應使用第二種方法。

Shubham B Vora
Shubham B Vora

專業技術內容撰寫人 | 專注於技術、人工智慧和程式設計 | 幫助品牌有效溝通

更新於:2024年9月10日

2K+瀏覽量

開啟你的職業生涯

完成課程獲得認證

立即開始
廣告