获取链表元素的Java程序
链表是一种线性数据结构,其中元素不存储在连续的内存位置。在这里,任务是获取 LinkedList 的元素。
1.我们可以使用get(int variable) 方法从 LinkedList 的特定索引访问元素:
在给定的示例中,我们使用了get(i) 方法。这里,该方法返回位于第i个索引处的元素。
句法:
LinkedList.get(int index)
参数:参数index是整数数据类型,指定要从 LinkedList 获取的元素的位置或索引。
返回值:该方法返回存在于参数index指定位置的元素。
Java
// Java program to get the elements of Linkedlist
import java.io.*;
import java.util.LinkedList;
class GFG {
public static void main(String[] args)
{
// Creating LinkedList
LinkedList gfg = new LinkedList();
// Adding values
gfg.add("GEEKS");
gfg.add("FOR");
gfg.add("GEEKS");
System.out.println("LinkedList Elements : ");
for (int i = 0; i < gfg.size(); i++) {
// get(i) returns element present at index i
System.out.println("Element at index " + i
+ " is: " + gfg.get(i));
}
}
}
Java
// Java program to iterate over linkedlist
// to extract elements of linkedlist
import java.io.*;
import java.util.LinkedList;
import java.util.Iterator;
class GFG {
public static void main(String[] args)
{
LinkedList gfg = new LinkedList();
// Adding elements
gfg.add("GEEKS");
gfg.add("FOR");
gfg.add("GEEKS");
// Create an object of Iterator
Iterator i = gfg.iterator();
System.out.print(
"The elements of the input LinkedList: \n");
int j = 0;
// has.next() returns true if there is a next
// element
while (i.hasNext()) {
System.out.print("The element at the index " + j
+ " ");
// next() returns the next element
String str = i.next();
System.out.print(str);
System.out.print(" \n");
++j;
}
}
}
Java
// Java program to iterate over the
// linkedlist using listIterator()
import java.io.*;
import java.util.LinkedList;
import java.util.ListIterator;
class GFG {
public static void main(String[] args)
{
LinkedList gfg = new LinkedList();
// Adding elements
gfg.add("GEEKS");
gfg.add("FOR");
gfg.add("GEEKS");
// Create an object of ListIterator
ListIterator li = gfg.listIterator();
System.out.print(
"The elements of the LinkedList: \n");
// hasNext() returns true if there is next element
int j = 0;
while (li.hasNext()) {
// next() returns the next element
System.out.print("The element at the index " + j
+ " ");
System.out.print(li.next());
System.out.print("\n");
++j;
}
--j;
// Now to show that ListIterator() can traverse in
// both the direction
System.out.print(
"\nThe elements of the LinkedList in Reverse order: \n");
// hasprevious() checks if there is a previous
// element
while (li.hasPrevious()) {
System.out.print("The element at the index " + j
+ " ");
// previous() returns the previous element
System.out.print(li.previous());
System.out.print("\n");
--j;
}
}
}
输出
LinkedList Elements :
Element at index 0 is: GEEKS
Element at index 1 is: FOR
Element at index 2 is: GEEKS
2.我们可以使用iterator()方法
- 要使用此方法,我们必须导入Java.util.Iterator包。
- 在这个方法中,我们可以遍历 LinkedList,然后相应地提取给定索引处的元素。
Java
// Java program to iterate over linkedlist
// to extract elements of linkedlist
import java.io.*;
import java.util.LinkedList;
import java.util.Iterator;
class GFG {
public static void main(String[] args)
{
LinkedList gfg = new LinkedList();
// Adding elements
gfg.add("GEEKS");
gfg.add("FOR");
gfg.add("GEEKS");
// Create an object of Iterator
Iterator i = gfg.iterator();
System.out.print(
"The elements of the input LinkedList: \n");
int j = 0;
// has.next() returns true if there is a next
// element
while (i.hasNext()) {
System.out.print("The element at the index " + j
+ " ");
// next() returns the next element
String str = i.next();
System.out.print(str);
System.out.print(" \n");
++j;
}
}
}
输出
The elements of the input LinkedList:
The element at the index 0 GEEKS
The element at the index 1 FOR
The element at the index 2 GEEKS
3.我们可以使用ListIterator()方法。
- ListIterator()是Iterator() 方法的子接口。
- 它为我们提供了访问列表元素的函数。
- 它是双向的,这意味着它允许我们在两个方向上迭代列表的元素。
- 要使用此方法,我们必须导入Java.util.ListIterator。
Java
// Java program to iterate over the
// linkedlist using listIterator()
import java.io.*;
import java.util.LinkedList;
import java.util.ListIterator;
class GFG {
public static void main(String[] args)
{
LinkedList gfg = new LinkedList();
// Adding elements
gfg.add("GEEKS");
gfg.add("FOR");
gfg.add("GEEKS");
// Create an object of ListIterator
ListIterator li = gfg.listIterator();
System.out.print(
"The elements of the LinkedList: \n");
// hasNext() returns true if there is next element
int j = 0;
while (li.hasNext()) {
// next() returns the next element
System.out.print("The element at the index " + j
+ " ");
System.out.print(li.next());
System.out.print("\n");
++j;
}
--j;
// Now to show that ListIterator() can traverse in
// both the direction
System.out.print(
"\nThe elements of the LinkedList in Reverse order: \n");
// hasprevious() checks if there is a previous
// element
while (li.hasPrevious()) {
System.out.print("The element at the index " + j
+ " ");
// previous() returns the previous element
System.out.print(li.previous());
System.out.print("\n");
--j;
}
}
}
输出
The elements of the LinkedList:
The element at the index 0 GEEKS
The element at the index 1 FOR
The element at the index 2 GEEKS
The elements of the LinkedList in Reverse order:
The element at the index 2 GEEKS
The element at the index 1 FOR
The element at the index 0 GEEKS