Java中的 AbstractList remove() 方法及示例
Java.util.AbstractList类的remove(int index)方法用于从特定位置或索引的抽象列表中删除元素。
句法:
AbstractList.remove(int index)
参数:参数索引是整数数据类型,并指定要从 AbstractList 中删除的元素的位置。
返回值:此方法返回刚刚从列表中删除的元素。
下面的程序说明了 AbstractList.remove(int index) 方法:
方案一:
// Java code to illustrate remove() method
import java.util.*;
import java.util.LinkedList;
public class AbstractListDemo {
public static void main(String args[])
{
// Creating an empty AbstractList
AbstractList
list = new LinkedList();
// Using add() method to add elements in the list
list.add("Geeks");
list.add("for");
list.add("Geeks");
list.add("10");
list.add("20");
// Output the list
System.out.println("AbstractList: " + list);
// Remove the head using remove()
list.remove(3);
// Print the final list
System.out.println("Final AbstractList: " + list);
}
}
输出:
AbstractList: [Geeks, for, Geeks, 10, 20]
Final AbstractList: [Geeks, for, Geeks, 20]
方案二:
// Java code to illustrate remove() when position of
// element is passed as parameter
import java.util.*;
import java.util.LinkedList;
public class AbstractListDemo {
public static void main(String args[])
{
// Creating an empty AbstractList
AbstractList list = new LinkedList();
// Use add() method to add elements in the list
list.add("Geeks");
list.add("for");
list.add("Geeks");
list.add("10");
list.add("20");
// Output the list
System.out.println("AbstractList:" + list);
// Remove the head using remove()
list.remove(0);
// Print the final list
System.out.println("Final AbstractList:" + list);
}
}
输出:
AbstractList:[Geeks, for, Geeks, 10, 20]
Final AbstractList:[for, Geeks, 10, 20]