📌  相关文章
📜  Java中的 CopyOnWriteArrayList set() 方法及示例

📅  最后修改于: 2022-05-13 01:54:33.497000             🧑  作者: Mango

Java中的 CopyOnWriteArrayList set() 方法及示例

CopyOnWriteArrayList 类中的set(E e)方法将指定索引处的元素替换为作为该方法的参数提供的元素。该方法返回已被新元素替换的元素。
句法:

public E set(int index, E element)

参数:该方法采用下面提到的两个参数:

  • 索引:包含新元素替换现有元素的位置。必须添加。
  • Element :包含要替换的新元素。

返回值:该方法返回已被替换的元素
异常:当方法的索引小于 0 或大于列表大小时,方法会抛出IndexOutOfBoundsException
下面是一些程序来说明 CopyOnWriteArrayList.set() 方法的使用:
方案一:

Java
// 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 arrayList
            = new CopyOnWriteArrayList();
 
        // 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 calling set() method:"
                           + returnValue);
 
        // print CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList "
                           + "after calling set():"
                           + arrayList);
    }
}


Java
// Program to illustrate the ArrayIndexOutOfBoundsException
 
import java.util.concurrent.CopyOnWriteArrayList;
 
public class CopyOnWriteArrayListDemo {
    public static void main(String[] args)
    {
 
        // creating an ArrayList
        CopyOnWriteArrayList arrayList
            = new CopyOnWriteArrayList();
 
        // 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 "
                               + "using set() method");
 
            // invoking the set() method
            String returnValue
                = arrayList.set(4, "toodles");
            // printing the returned value
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}



方案二:

Java

// Program to illustrate the ArrayIndexOutOfBoundsException
 
import java.util.concurrent.CopyOnWriteArrayList;
 
public class CopyOnWriteArrayListDemo {
    public static void main(String[] args)
    {
 
        // creating an ArrayList
        CopyOnWriteArrayList arrayList
            = new CopyOnWriteArrayList();
 
        // 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 "
                               + "using set() method");
 
            // invoking the set() method
            String returnValue
                = arrayList.set(4, "toodles");
            // printing the returned value
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}