ArrayList类remove()方法 (ArrayList Class remove() method)
Syntax:
句法:
public boolean remove(Object obj);
public T remove(int indices);
remove() method is available in java.util package.
remove()方法在java.util包中可用。
remove(Object obj) method is used to delete the first occurrence of the given object from this Arraylist when exists.
remove(Object obj)方法用于从存在的Arraylist中删除给定对象的第一次出现。
remove(int indices) method is used to delete the element at the given indices from this Arraylist & shift other elements to the left side.
remove(int index)方法用于从此Arraylist中删除给定索引处的元素,并将其他元素移到左侧。
remove(Object obj) method does not throw an exception at the time of removing an element.
remove(Object obj)方法在删除元素时不会引发异常。
remove(obj int indices) method may throw an exception at the time of removing an element at the given indices.
remove(obj int index)方法可能会在移除给定索引处的元素时引发异常。
IndexOutOfBoundsException: This exception may throw when the given indices are not in a range.
IndexOutOfBoundsException :如果给定索引不在范围内,则可能引发此异常。
These are non-static methods, it is accessible with class object & if we try to access these methods with the class name then we will get an error.
这些是非静态方法,可通过类对象访问;如果尝试使用类名称访问这些方法,则会收到错误消息。
Parameter(s):
参数:
In the first case, remove(Object obj)
在第一种情况下, 删除(Object obj)
- Object obj – represents the object to be removed from this Arraylist when exists.
- Object obj –表示要从此Arraylist中删除的对象(如果存在)。
In the second case, remove (int indices)
在第二种情况下, 删除(int索引)
- int indices – represent the indices of the element to be removed from this Arraylist.
- int index –表示要从此Arraylist中删除的元素的索引。
Return value:
返回值:
In the first case, the return type of the method is boolean, it returns true if the given object is to be removed from the Arraylist when exists.
在第一种情况下,该方法的返回类型为boolean ,如果给定对象(如果存在)要从Arraylist中移除,则返回true 。
In the second case, the return type of the method is T, it returns the removed element from this Arraylist.
在第二种情况下,方法的返回类型为T ,它返回从此Arraylist中移除的元素。
Example:
例:
// Java program to demonstrate the example
// of remove() method of ArrayList.
import java.util.*;
public class RemoveOfArrayList {
public static void main(String[] args) {
// Create an ArrayList with initial
// capacity of storing elements
ArrayList < String > arr_l = new ArrayList < String > (10);
// By using add() method is to add
// elements in this ArrayList
arr_l.add("C");
arr_l.add("C++");
arr_l.add("JAVA");
arr_l.add("DOTNET");
arr_l.add("PHP");
// Display ArrayList
System.out.println("arr_l :" + arr_l);
// By using remove(Object) method is to remove
// the given object from this ArrayList
arr_l.remove("C++");
// Display ArrayList
System.out.println("arr_l.remove(C++) :" + arr_l);
// By using remove(int) method is to remove
// the object at the given index from this ArrayList
arr_l.remove(3);
// Display ArrayList
System.out.println("arr_l.remove(3) : " + arr_l);
}
}
Output
输出量
arr_l :[C, C++, JAVA, DOTNET, PHP]
arr_l.remove(C++) :[C, JAVA, DOTNET, PHP]
arr_l.remove(3) : [C, JAVA, DOTNET]
翻译自: https://www.includehelp.com/java/arraylist-remove-method-with-example.aspx