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:如果此列表不支持set操作
  • ClassCastException:如果指定元素的类防止将其添加到此列表中
  • NullPointerException:如果指定的元素为null并且此列表不允许使用null元素
  • 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 代码演示 set()
  
import java.io.*;
import java.util.*;
  
public class AbstractSequentialListDemo {
    public static void main(String args[])
    {
        // 创建空的 AbstractSequentialList
        AbstractSequentialList<String> list
            = new LinkedList<String>();
  
        // 使用 add() 方法向 List 中添加元素
        list.add("Geeks");
        list.add("for");
        list.add("Geeks");
        list.add("10");
        list.add("20");
  
        // 显示 LinkedList
        System.out.println("AbstractSequentialList:"
                           + list);
  
        // 使用 set() 方法将第 10 个元素替换为 GFG(不存在第 10 个元素)
        System.out.println("试图将第 10 个元素替换为 GFG");
  
        try {
            list.set(10, "GFG");
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
AbstractSequentialList:[Geeks, for, Geeks, 10, 20]
试图将第 10 个元素替换为 GFG
java.lang.IndexOutOfBoundsException: Index: 10, Size: 5

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程