當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Stack set()用法及代碼示例


Java Stack的set()方法用於將使用Stack類創建的堆棧中的任何特定元素替換為另一個元素。這可以通過在set()方法的參數中指定要替換的元素的位置和新元素來完成。

用法:

public E set(int index, Object element)

參數:該函數接受兩個參數,如上麵的語法所示,如下所述。


  • index:這是整數類型,是指要從堆棧中替換的元素的位置。
  • element:這是新元素,現有元素將被替換,並且與堆棧具有相同的對象類型。

返回值:該方法從堆棧中返回由新值替換的先前值。

異常:此方法引發以下異常:

  • UnsupportedOperationException:如果此堆棧不支持設置操作
  • ClassCastException:如果指定元素的類阻止將其添加到此堆棧中
  • NullPointerException :如果指定的元素為null,並且此堆棧不允許使用null元素
  • IllegalArgumentException:如果指定元素的某些屬性阻止將其添加到此堆棧中
  • IndexOutOfBoundsException:如果索引超出範圍(索引= size())

以下示例程序旨在說明Java.util.Stack.set()方法:

示例1:

// Java code to illustrate set() 
  
import java.io.*; 
import java.util.*; 
  
public class StackDemo { 
    public static void main(String args[]) 
    { 
        // Creating an empty Stack 
        Stack<String> stack 
            = new Stack<String>(); 
  
        // Use add() method to add elements in the stack 
        stack.add("Geeks"); 
        stack.add("for"); 
        stack.add("Geeks"); 
        stack.add("10"); 
        stack.add("20"); 
  
        // Displaying the linkedstack 
        System.out.println("Stack:"
                           + stack); 
  
        // Using set() method to replace Geeks with GFG 
        System.out.println("The Object that is replaced is: "
                           + stack.set(2, "GFG")); 
  
        // Using set() method to replace 20 with 50 
        System.out.println("The Object that is replaced is: "
                           + stack.set(4, "50")); 
  
        // Displaying the modified linkedstack 
        System.out.println("The new Stack is:"
                           + stack); 
    } 
}
輸出:
Stack:[Geeks, for, Geeks, 10, 20]
The Object that is replaced is: Geeks
The Object that is replaced is: 20
The new Stack is:[Geeks, for, GFG, 10, 50]

示例2:演示IndexOutOfBoundException

// Java code to illustrate set() 
  
import java.io.*; 
import java.util.*; 
  
public class StackDemo { 
    public static void main(String args[]) 
    { 
        // Creating an empty Stack 
        Stack<String> stack 
            = new Stack<String>(); 
  
        // Use add() method to add elements in the stack 
        stack.add("Geeks"); 
        stack.add("for"); 
        stack.add("Geeks"); 
        stack.add("10"); 
        stack.add("20"); 
  
        // Displaying the linkedstack 
        System.out.println("Stack:"
                           + stack); 
  
        // Using set() method to replace 10th with GFG 
        // and the 10th element does not exist 
        System.out.println("Trying to replace 10th "
                           + "element with GFG"); 
  
        try { 
            stack.set(10, "GFG"); 
        } 
        catch (Exception e) { 
            System.out.println(e); 
        } 
    } 
}
輸出:
Stack:[Geeks, for, Geeks, 10, 20]
Trying to replace 10th element with GFG
java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 10


相關用法


注:本文由純淨天空篩選整理自Code_r大神的英文原創作品 Stack set() method in Java with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。