Java AbstractSequentialList set(int, Object)方法及实例

Java AbstractSequentialList set(int, Object)方法及实例

Java AbstractSequentialList的 set() 方法用于用AbstractSequentialList类创建的列表中的任何特定元素替换成另一个元素。这可以通过在set()方法的参数中指定要替换的元素和新元素的位置来完成。

语法

public E set(int index, Object element)

参数: 该函数接受两个参数,如上面的语法所示,并描述如下。

  • index : 这是一个整数类型的参数,是指要从列表中替换的元素的位置。
  • element : 这是一个新的元素,现有的元素将被替换,与列表的对象类型相同。

返回值 :该方法返回被替换为新值的列表中的先前值。

异常。该方法会抛出以下异常。

  • UnsupportedOperationException : 如果这个列表不支持设置操作。
  • ClassCastException:如果指定的元素的类别阻止它被添加到这个列表中。
  • NullPointerException : 如果指定的元素是空的,而这个列表不允许空的元素。
  • IllegalArgumentException:如果指定元素的某些属性阻止它被添加到这个列表中。
  • IndexOutOfBoundsException:如果索引超出了范围(index = size())。

下面的程序说明了Java.util.AbstractSequentialList.set()方法。

例子 1:

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

输出。

AbstractSequentialList:[Geeks, for, Geeks, 10, 20]
The Object that is replaced is: Geeks
The Object that is replaced is: 20
The new AbstractSequentialList is:[Geeks, for, GFG, 10, 50]

例2:演示IndexOutOfBoundException

// Java code to illustrate set()
  
import java.io.*;
import java.util.*;
  
public class AbstractSequentialListDemo {
    public static void main(String args[])
    {
        // Creating an empty AbstractSequentialList
        AbstractSequentialList<String> list
            = new LinkedList<String>();
  
        // Use add() method to add elements in the list
        list.add("Geeks");
        list.add("for");
        list.add("Geeks");
        list.add("10");
        list.add("20");
  
        // Displaying the linkedlist
        System.out.println("AbstractSequentialList:"
                           + list);
  
        // 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 {
            list.set(10, "GFG");
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

输出。

AbstractSequentialList:[Geeks, for, Geeks, 10, 20]
Trying to replace 10th element with GFG
java.lang.IndexOutOfBoundsException: Index: 10, Size: 5

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程