从Java ArrayList 中的指定索引中删除元素
Java.util.ArrayList 类中的remove(int index)方法删除此列表中指定位置的元素并将任何后续元素向左移动(即从它们的索引中减去一个)。
句法 :
public removed_element remove(int index)
参数:要删除的元素的索引。
返回类型:此方法返回从列表中删除的元素。
异常:如果索引超出范围,此方法将抛出IndexOutOfBoundsException 。
Java
// Java program to remove an element
// from an specified index from
// an ArrayList.
import java.util.ArrayList;
public class GFG {
public static void main(String[] arg)
{
// creating an empty ArrayList with an initial
// capacity of 5
ArrayList flower = new ArrayList(5);
// using add() method to add elements in the
// ArrayList flower
flower.add("red-rose");
flower.add("tulip");
flower.add("sun-flower");
flower.add("marie-gold");
flower.add("orchid");
// printing the size of the ArrayList flower
System.out.println("Size of list: "
+ flower.size());
// printing the ArrayList flower
System.out.println("Flower ArrayList = " + flower);
// Removing element at 3rd position from ArrayList
// flower
System.out.println(
"Removing element at index = 2 ");
flower.remove(2);
System.out.println("After removing element");
// printing the size of the ArrayList flower
System.out.println("Size of list: "
+ flower.size());
// printing the ArrayList flower
System.out.println("Flower ArrayList = " + flower);
}
}
Java
// Java program to show the exception
// of remove() method
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String[] args) {
// create an empty array list with an initial capacity
ArrayList arrlist = new ArrayList(5);
// use add() method to add elements in the deque
arrlist.add(20);
arrlist.add(15);
arrlist.add(30);
arrlist.add(45);
System.out.println("Size of list: " + arrlist.size());
// let us print all the elements available in list
for (Integer number : arrlist) {
System.out.println("Number = " + number);
}
// Removes element at 5th position
// which is not present in the list
// and will therefore give IndexOutOfBound exception
arrlist.remove(4);
System.out.println("Now, Size of list: " + arrlist.size());
// let us print all the elements available in list
for (Integer number : arrlist) {
System.out.println("Number = " + number);
}
}
}
输出
Size of list: 5
Flower ArrayList = [red-rose, tulip, sun-flower, marie-gold, orchid]
Removing element at index = 2
After removing element
Size of list: 4
Flower ArrayList = [red-rose, tulip, marie-gold, orchid]
当我们尝试删除大于或等于数组列表大小的索引处的元素时,编辑器会在运行时给我们一个IndexOutOfBound 异常。
Java
// Java program to show the exception
// of remove() method
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String[] args) {
// create an empty array list with an initial capacity
ArrayList arrlist = new ArrayList(5);
// use add() method to add elements in the deque
arrlist.add(20);
arrlist.add(15);
arrlist.add(30);
arrlist.add(45);
System.out.println("Size of list: " + arrlist.size());
// let us print all the elements available in list
for (Integer number : arrlist) {
System.out.println("Number = " + number);
}
// Removes element at 5th position
// which is not present in the list
// and will therefore give IndexOutOfBound exception
arrlist.remove(4);
System.out.println("Now, Size of list: " + arrlist.size());
// let us print all the elements available in list
for (Integer number : arrlist) {
System.out.println("Number = " + number);
}
}
}
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 4 out of bounds for length 4