類CopyOnWriteArrayList類中的set(E e)方法將指定索引處的元素替換為作為該方法的參數提供的元素。該方法返回已被新元素替換的元素。
用法:
public E set(int index, E element)
參數:該方法采用以下兩個參數:
- Index:包含新元素替換現有元素的位置。必須添加。
- Element:包含要替換的新元素。
返回值:該方法返回已替換的元素。
異常:當方法的索引小於0或大於列表的大小時,將發生方法拋出IndexOutOfBoundsException。
下麵是一些程序,用於說明CopyOnWriteArrayList.set()方法的使用:
示例1:
// Program to illustrate the use of set() method
import java.util.concurrent.CopyOnWriteArrayList;
public class CopyOnWriteArrayListDemo {
public static void main(String[] args)
{
// creating an ArrayList
CopyOnWriteArrayList<String> arrayList
= new CopyOnWriteArrayList<String>();
// Adding elements to the list
arrayList.add(0, "geeks");
arrayList.add(1, "for");
arrayList.add(2, "geeksforgeeks");
// before invoking the set() method
System.out.println("CopyOnWriteArrayList: "
+ arrayList);
// invoking the set() method
String returnValue
= arrayList.set(0, "toodles");
// printing the returned value
System.out.println("The value returned "
+ "on caling set() method:"
+ returnValue);
// print CopyOnWriteArrayList
System.out.println("CopyOnWriteArrayList "
+ "after calling set():"
+ arrayList);
}
}
輸出:
CopyOnWriteArrayList: [geeks, for, geeksforgeeks] The value returned on caling set() method: geeks CopyOnWriteArrayList after calling set(): [toodles, for, geeksforgeeks]
示例2:
// Program to illustrate the ArrayIndexOutOfBoundsException
import java.util.concurrent.CopyOnWriteArrayList;
public class CopyOnWriteArrayListDemo {
public static void main(String[] args)
{
// creating an ArrayList
CopyOnWriteArrayList<String> arrayList
= new CopyOnWriteArrayList<String>();
// Adding elements to the list
arrayList.add(0, "geeks");
arrayList.add(1, "for");
arrayList.add(2, "geeksforgeeks");
// before invoking the set() method
System.out.println("CopyOnWriteArrayList: "
+ arrayList);
try {
System.out.println("Trying to add "
+ "element at index 4 "
+ "usiung set() method");
// invoking the set() method
String returnValue
= arrayList.set(4, "toodles");
// printing the returned value
}
catch (Exception e) {
System.out.println(e);
}
}
}
輸出:
CopyOnWriteArrayList: [geeks, for, geeksforgeeks] Trying to add element at index 4 usiung set() method java.lang.ArrayIndexOutOfBoundsException: 4
相關用法
- Java CopyOnWriteArrayList remove()用法及代碼示例
- Java CopyOnWriteArrayList addAllAbsent()用法及代碼示例
- Java CopyOnWriteArrayList addAll()用法及代碼示例
- Java CopyOnWriteArrayList retainAll()用法及代碼示例
- Java CopyOnWriteArrayList equals()用法及代碼示例
- Java CopyOnWriteArrayList forEach()用法及代碼示例
- Java CopyOnWriteArrayList removeIf()用法及代碼示例
- Java CopyOnWriteArrayList subList()用法及代碼示例
- Java CopyOnWriteArrayList removeAll()用法及代碼示例
- Java CopyOnWriteArrayList get()用法及代碼示例
- Java CopyOnWriteArrayList add()用法及代碼示例
- Java CopyOnWriteArrayList contains()用法及代碼示例
- Java CopyOnWriteArrayList lastIndexOf()用法及代碼示例
- Java CopyOnWriteArrayList listIterator()用法及代碼示例
- Java CopyOnWriteArrayList size()用法及代碼示例
注:本文由純淨天空篩選整理自DeepaliK1大神的英文原創作品 CopyOnWriteArrayList set() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。