Java LinkedList对象的get(int index)方法与示例
LinkedList对象的get(int索引)方法
包java.util.LinkedList.get(intindex)中提供了此方法。
此方法用于在链接列表的指定位置检索对象或元素。
语法:
Object get(int index){
}参数:
在链接列表的方法中,我们仅传递一个对象作为参数,该参数是返回元素的位置。
返回值:
该方法的返回类型为Object,这意味着该方法在链接列表的指定位置返回一个元素。
Java程序演示LinkedListget()方法的示例
import java.util.LinkedList;
public class LinkList {
public static void main(String[] args) {
LinkedList list = new LinkedList();
//add()在列表中添加元素的方法
list.add(10);
list.add(20);
list.add(30);
list.add(40);
list.add(50);
//当前列表输出
System.out.println("The Current list is:" + list);
//检索元素
System.out.println("The indexed element of the linked list is:" + list.get(2));
//新列表输出
System.out.println("The new list is:" + list);
}
}输出结果
D:\Programs>javac LinkList.java D:\Programs>java LinkList The Current list is:[10, 20, 30, 40, 50] The indexed element of the linked list is:30 The new list is:[10, 20, 30, 40, 50]