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


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


java.util.ArrayList類的set()方法用於使用指定元素替換此列表中指定位置的元素。

用法:

public E set(int index, E element)

參數:此方法將以下參數作為參數。


  • index-替換元素的索引
  • element-要存儲在指定位置的元素

返回值:此方法返回先前位於指定位置的元素。

異常:如果索引不在ArrayList的大小範圍內,則此方法將引發IndexOutOfBoundsException。

以下示例說明了set()方法。

示例1:

// Java program to demonstrate 
// set() method 
// for Integer value 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) throws Exception 
    { 
        try { 
  
            // Creating object of ArrayList<Integer> 
            ArrayList<Integer> 
                arrlist = new ArrayList<Integer>(); 
  
            // Populating arrlist1 
            arrlist.add(1); 
            arrlist.add(2); 
            arrlist.add(3); 
            arrlist.add(4); 
            arrlist.add(5); 
  
            // print arrlist 
            System.out.println("Before operation: "
                               + arrlist); 
  
            // Replacing element at the index 3 with 30 
            // using method set() 
            int i = arrlist.set(3, 30); 
  
            // Print the modified arrlist 
            System.out.println("After operation: "
                               + arrlist); 
  
            // Print the Replaced element 
            System.out.println("Replaced element: "
                               + i); 
        } 
  
        catch (IndexOutOfBoundsException e) { 
            System.out.println("Exception thrown: "
                               + e); 
        } 
    } 
}
輸出:
Before operation : [1, 2, 3, 4, 5]
After operation : [1, 2, 3, 30, 5]
Replaced element : 4

示例2:對於IndexOutOfBoundsException

// Java program to demonstrate 
// set() method 
// for IndexOutOfBoundsException 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) throws Exception 
    { 
        try { 
  
            // Creating object of ArrayList<Integer> 
            ArrayList<Integer> 
                arrlist = new ArrayList<Integer>(); 
  
            // Populating arrlist1 
            arrlist.add(1); 
            arrlist.add(2); 
            arrlist.add(3); 
            arrlist.add(4); 
            arrlist.add(5); 
  
            // print arrlist 
            System.out.println("Before operation : "
                               + arrlist); 
  
            // Replacing element at the index 7 with 30 
            // using method set() 
            System.out.println("\nTrying to Replace"
                               + " the element at"
                               + " (index > Capacity) "); 
            int i = arrlist.set(7, 30); 
  
            // Print the modified arrlist 
            System.out.println("After operation: "
                               + arrlist); 
  
            // Print the Replaced element 
            System.out.println("Replaced element: "
                               + i); 
        } 
  
        catch (IndexOutOfBoundsException e) { 
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
輸出:
Before operation : [1, 2, 3, 4, 5]

Trying to Replace the element at (index > Capacity) 
Exception thrown : java.lang.IndexOutOfBoundsException: Index: 7, Size: 5


相關用法


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