类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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。