📜  如何在Java中迭代 LinkedList?

📅  最后修改于: 2022-05-13 01:54:41.505000             🧑  作者: Mango

如何在Java中迭代 LinkedList?

Java中的 LinkedList 基本上是Java.util 包中存在的集合框架的一部分。它是 LinkedList 数据结构的实现,它以非连续的方式在内存中存储元素。

迭代 LinkedList 的五种方法是:

  1. 使用 for 循环
  2. 使用while循环
  3. 使用增强的 for 循环
  4. 使用迭代器
  5. 使用 forEach() 方法

方法 1:使用 For循环

Java
// Java program for iterating the LinkedList
// using For loop
  
import java.util.LinkedList;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Creating a LinkedList of Integer type
        LinkedList linkedList = new LinkedList<>();
  
        // Inserting some Integer values to our LinkedList
        linkedList.add(40);
        linkedList.add(44);
        linkedList.add(80);
        linkedList.add(9);
  
        // LinkedList after insertions: [40, 44, 80, 9]
  
        // Calling the function to iterate our LinkedList
        iterateUsingForLoop(linkedList);
    }
  
    // Function to iterate the LinkedList using a simple for
    // loop
    public static void
             iterateUsingForLoop(LinkedList linkedList)
    {
  
        System.out.print(
            "Iterating the LinkedList using a simple for loop : ");
  
        for (int i = 0; i < linkedList.size(); i++) {
            System.out.print(linkedList.get(i) + " ");
        }
    }
}


Java
// Java program for iterating the LinkedList
// using while loop
  
import java.util.LinkedList;
  
public class GFG {
    public static void main(String[] args) {
  
        // Creating a LinkedList of Character type
        LinkedList vowels = new LinkedList<>();
  
        // Inserting some Character values to our LinkedList
        vowels.add('a');
        vowels.add('e');
        vowels.add('i');
        vowels.add('o');
        vowels.add('u');
  
        // LinkedList after insertions: ['a', 'e', 'i', 'o', 'u']
  
        // calling the function to iterate our LinkedList
        iterateUsingWhileLoop(vowels);
    }
  
    // Function to iterate our LinkedList using while loop
    public static void iterateUsingWhileLoop(LinkedList vowels){
  
        System.out.print("Iterating the LinkedList using while loop : ");
  
        int i=0;
        
        while(i


Java
// Java program for iterating the LinkedList
// using Enhanced For loop
  
import java.util.LinkedList;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Creating a LinkedList of String type
        LinkedList linkedList = new LinkedList<>();
  
        // Inserting some String values to our LinkedList
        linkedList.add("Geeks");
        linkedList.add("for");
        linkedList.add("Geeks");
  
        // LinkedList after insertions: ["Geeks", "for",
        // "Geeks]
  
        // Calling the function to iterate our LinkedList
        iterateUsingEnhancedForLoop(linkedList);
    }
  
    // Function to display LinkedList using Enhanced for
    // loop
    public static void iterateUsingEnhancedForLoop(LinkedList linkedList)
    {
  
        System.out.print(
            "Iterating the LinkedList using enhanced for loop : ");
  
        for (String listElement : linkedList) {
            System.out.print(listElement + " ");
        }
    }
}


Java
// Java program for iterating the LinkedList
// using Iterator
  
import java.util.Iterator;
  
// Importing LinkedList class from
// java.util package
import java.util.LinkedList;
  
public class GFG {
    public static void main(String[] args) {
  
        // Creating a LinkedList of Integer Type
        LinkedList linkedList = new LinkedList<>();
  
        // Inserting some Integer values to our LinkedList
        linkedList.add(5);
        linkedList.add(100);
        linkedList.add(41);
        linkedList.add(40);
        linkedList.add(7);
  
        // LinkedList after insertions : [5, 100, 41, 40, 7]
  
        // Calling the function to iterate our LinkedList
        iterateUsingIterator(linkedList);
    }
  
    // Function to iterate the Linked List using Iterator
    public static void iterateUsingIterator(LinkedList linkedList){
  
        System.out.print("Iterating the LinkedList using Iterator : ");
  
        // Creating an Iterator to our current LinkedList
        Iterator it = linkedList.iterator();
  
        // Inside the while loop we check if the next element
        // exists or not if the next element exists then we print
        // the next element and move to it otherwise we come out
        // of the loop
        
        // hasNext() method return boolean value
          // It returns true when the next element
          // exists otherwise returns false
        while(it.hasNext()){
  
            // next() return the next element in the iteration
            System.out.print(it.next() + " ");
        }
  
    }
}


Java
// Java program for iterating the LinkedList
// using For Each loop
  
import java.util.LinkedList;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Creating a LinkedList of Integer Type
        LinkedList linkedList = new LinkedList<>();
  
        // Inserting some Integer values to our LinkedList
        linkedList.add(1);
        linkedList.add(2);
        linkedList.add(3);
        linkedList.add(4);
        linkedList.add(5);
  
        // LinkedList after insertions: [1, 2, 3, 4, 5]
  
        // Calling the function to iterate our LinkedList
        iterateUsingForEach(linkedList);
    }
  
    public static void
            iterateUsingForEach(LinkedList linkedList)
    {
  
        System.out.print(
            "Iterating the LinkedList using for each function : ");
  
        // Calling the forEach function to iterate through
        // all the elements inside the Linked List
        linkedList.forEach(
            (element) -> System.out.print(element + " "));
    }
}


输出
Iterating the LinkedList using a simple for loop : 40 44 80 9

方法二:使用while循环

我们可以使用 while 循环以与使用 for 循环非常相似的方式来迭代我们的 LinkedList。

Java

// Java program for iterating the LinkedList
// using while loop
  
import java.util.LinkedList;
  
public class GFG {
    public static void main(String[] args) {
  
        // Creating a LinkedList of Character type
        LinkedList vowels = new LinkedList<>();
  
        // Inserting some Character values to our LinkedList
        vowels.add('a');
        vowels.add('e');
        vowels.add('i');
        vowels.add('o');
        vowels.add('u');
  
        // LinkedList after insertions: ['a', 'e', 'i', 'o', 'u']
  
        // calling the function to iterate our LinkedList
        iterateUsingWhileLoop(vowels);
    }
  
    // Function to iterate our LinkedList using while loop
    public static void iterateUsingWhileLoop(LinkedList vowels){
  
        System.out.print("Iterating the LinkedList using while loop : ");
  
        int i=0;
        
        while(i
输出
Iterating the LinkedList using while loop : a e i o u

方法 3:使用增强的 for 循环

使用增强的 for 循环,我们可以顺序迭代 LinkedList。在我们访问完所有元素后,增强的 for 循环的执行结束。让我们看一个使用增强的 for 循环迭代 LinkedList 的示例。

Java

// Java program for iterating the LinkedList
// using Enhanced For loop
  
import java.util.LinkedList;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Creating a LinkedList of String type
        LinkedList linkedList = new LinkedList<>();
  
        // Inserting some String values to our LinkedList
        linkedList.add("Geeks");
        linkedList.add("for");
        linkedList.add("Geeks");
  
        // LinkedList after insertions: ["Geeks", "for",
        // "Geeks]
  
        // Calling the function to iterate our LinkedList
        iterateUsingEnhancedForLoop(linkedList);
    }
  
    // Function to display LinkedList using Enhanced for
    // loop
    public static void iterateUsingEnhancedForLoop(LinkedList linkedList)
    {
  
        System.out.print(
            "Iterating the LinkedList using enhanced for loop : ");
  
        for (String listElement : linkedList) {
            System.out.print(listElement + " ");
        }
    }
}
输出
Iterating the LinkedList using enhanced for loop : Geeks for Geeks

方法四:使用迭代器

  • 要使用迭代器迭代 LinkedList,我们首先为当前列表创建一个迭代器,并使用next()方法继续打印下一个元素,直到下一个元素存在于 LinkedList 中。
  • 我们使用hasNext()方法检查 LinkedList 是否包含下一个元素。

Java

// Java program for iterating the LinkedList
// using Iterator
  
import java.util.Iterator;
  
// Importing LinkedList class from
// java.util package
import java.util.LinkedList;
  
public class GFG {
    public static void main(String[] args) {
  
        // Creating a LinkedList of Integer Type
        LinkedList linkedList = new LinkedList<>();
  
        // Inserting some Integer values to our LinkedList
        linkedList.add(5);
        linkedList.add(100);
        linkedList.add(41);
        linkedList.add(40);
        linkedList.add(7);
  
        // LinkedList after insertions : [5, 100, 41, 40, 7]
  
        // Calling the function to iterate our LinkedList
        iterateUsingIterator(linkedList);
    }
  
    // Function to iterate the Linked List using Iterator
    public static void iterateUsingIterator(LinkedList linkedList){
  
        System.out.print("Iterating the LinkedList using Iterator : ");
  
        // Creating an Iterator to our current LinkedList
        Iterator it = linkedList.iterator();
  
        // Inside the while loop we check if the next element
        // exists or not if the next element exists then we print
        // the next element and move to it otherwise we come out
        // of the loop
        
        // hasNext() method return boolean value
          // It returns true when the next element
          // exists otherwise returns false
        while(it.hasNext()){
  
            // next() return the next element in the iteration
            System.out.print(it.next() + " ");
        }
  
    }
}
输出
Iterating the LinkedList using Iterator : 5 100 41 40 7

方法 5:使用 forEach() 方法

  • forEach() 方法是在 Java8 中引入的。
  • 它为 Iterable 的每个元素执行指定的任务,直到处理完所有元素或操作抛出异常。

句法:

public void forEach(Consumer action)

Java

// Java program for iterating the LinkedList
// using For Each loop
  
import java.util.LinkedList;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Creating a LinkedList of Integer Type
        LinkedList linkedList = new LinkedList<>();
  
        // Inserting some Integer values to our LinkedList
        linkedList.add(1);
        linkedList.add(2);
        linkedList.add(3);
        linkedList.add(4);
        linkedList.add(5);
  
        // LinkedList after insertions: [1, 2, 3, 4, 5]
  
        // Calling the function to iterate our LinkedList
        iterateUsingForEach(linkedList);
    }
  
    public static void
            iterateUsingForEach(LinkedList linkedList)
    {
  
        System.out.print(
            "Iterating the LinkedList using for each function : ");
  
        // Calling the forEach function to iterate through
        // all the elements inside the Linked List
        linkedList.forEach(
            (element) -> System.out.print(element + " "));
    }
}
输出
Iterating the LinkedList using for each function : 1 2 3 4 5