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