Java AbstractSequentialList remove()方法及示例
AbstractSequentialList的 remove(int index) 方法用于从抽象顺序列表的特定位置或索引中删除一个元素。
语法
AbstractSequentialList.remove(int index)
参数: 参数index为整数数据类型,指定要从AbstractSequentialList中移除的元素的位置。
返回值: 该方法返回刚刚从列表中删除的元素。
以下程序说明了AbstractSequentialList.remove(int index)方法。
程序1 :
// 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<String>
absqlist = new LinkedList<String>();
// 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]
程序 2:
// 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<String>
absqlist = new LinkedList<String>();
// 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]
极客教程