如何替换Java ArrayList 中的元素?
要替换Java ArrayList 中的元素,请使用Java.util的set()方法。可以使用 ArrayList 类。 set() 方法接受两个参数 - 必须替换的元素的索引和新元素的索引。 ArrayList 的索引是从零开始的。因此,要替换第一个元素,0 应该是作为参数传递的索引。
宣言:
public Object set(int index, Object element)
返回值:位于指定索引处的元素
异常抛出: 索引出界异常
当索引超出范围时会发生这种情况。
index < 0 or index >= size()
执行:
在这里,我们将提出 2 个示例,其中一个我们将在边界内设置索引,而在另一个示例中,我们将索引设置在边界外。
示例 1:索引在边界内的位置
Java
// Java program to demonstrate set() Method of ArrayList
// Where Index is Within Bound
// Importing required classes
import java.io.*;
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args) {
// Try block to check for exceptions
try {
// Creating an object of Arraylist class
ArrayList list = new ArrayList<>();
// Adding elements to the List
// using add() method
// Custom input elements
list.add("A");
list.add("B");
list.add("C");
list.add("D");
// Print all the elements added in the above object
System.out.println(list);
// 2 is the index of the element "C".
//"C" will be replaced by "E"
list.set(2, "E");
// Printing the newly updated List
System.out.println(list);
}
// Catch block to handle the exceptions
catch (Exception e) {
// Display the exception on the console
System.out.println(e);
}
}
}
Java
// Java program to demonstrate set() Method of ArrayList
// Where Index is Out of Bound
// Importing required classes
import java.io.*;
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args) {
// Try block to check for exceptions
try {
// Creating an object of Arraylist class
ArrayList list = new ArrayList<>();
// Adding elements to the List
// using add() method
// Custom input elements
list.add("A");
list.add("B");å
list.add("C");
list.add("D");
// Print all the elements added in the above object
System.out.println(list);
// Settijg the element at the 6 th index which
// does not exist in our input list object
list.set(6);
// Printing the newly updated List
System.out.println(list);
}
// Catch block to handle the exceptions
catch (Exception e) {
// Display the exception on the console
System.out.println(e);
}
}
}
输出
[A, B, C, D]
[A, B, E, D]
示例 2:索引越界的地方
Java
// Java program to demonstrate set() Method of ArrayList
// Where Index is Out of Bound
// Importing required classes
import java.io.*;
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args) {
// Try block to check for exceptions
try {
// Creating an object of Arraylist class
ArrayList list = new ArrayList<>();
// Adding elements to the List
// using add() method
// Custom input elements
list.add("A");
list.add("B");å
list.add("C");
list.add("D");
// Print all the elements added in the above object
System.out.println(list);
// Settijg the element at the 6 th index which
// does not exist in our input list object
list.set(6);
// Printing the newly updated List
System.out.println(list);
}
// Catch block to handle the exceptions
catch (Exception e) {
// Display the exception on the console
System.out.println(e);
}
}
}
输出: