Java中的 AbstractSequentialList get() 方法及示例
AbstractSequentialList 的get()方法用于从AbstractSequentialList中获取或检索特定索引处的元素。
句法:
AbstractSequentialList.get(int index)
参数:参数索引是整数数据类型,它指定要从 AbstractSequentialList 中获取的元素的位置或索引。
返回值:该方法返回存在于参数index指定位置的元素。
下面的程序说明了Java.util.AbstractSequentialList.get() 方法:
示例 1:
// Java code to illustrate get() 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();
// Use add() method to add elements
absqlist.add("Geeks");
absqlist.add("for");
absqlist.add("Geeks");
absqlist.add("10");
absqlist.add("20");
// Displaying the list
System.out.println("AbstractSequentialList:"
+ absqlist);
// Fetching the specific element from the list
System.out.println("The element is: "
+ absqlist.get(2));
}
}
输出:
AbstractSequentialList:[Geeks, for, Geeks, 10, 20]
The element is: Geeks
示例 2:
// Java code to illustrate get() 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();
// Use add() method to add elements
absqlist.add(1);
absqlist.add(2);
absqlist.add(3);
absqlist.add(4);
absqlist.add(5);
// Displaying the list
System.out.println("AbstractSequentialList:"
+ absqlist);
// Fetching the specific element from the list
System.out.println("The element is: "
+ absqlist.get(4));
}
}
输出:
AbstractSequentialList:[1, 2, 3, 4, 5]
The element is: 5