📜  使用循环在Java中迭代列表

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

使用循环在Java中迭代列表

在本文中,我们将了解如何遍历 List。在Java中,List 是 Collection 框架的一个接口。 List 可以是各种类型,例如 ArrayList、Stack、LinkedList 和 Vector。有多种方法可以遍历Java列表,但在这里我们将仅讨论使用循环的遍历。因此,有标准的三个遍历可用,因此确实存在三种方法,但是随着Java 8 和流的引入,其他方法确实出现了。因此,下面将讨论所有四种方法:

方法:

  1. For循环方法
  2. 虽然方法
  3. For-each 循环方法
  4. Java 8 的 for-each 循环

执行:

方法一:使用for循环

For 循环是最常见的流量控制循环。 For 循环使用一个变量来遍历列表。

例子

Java
// Java Program to Iterate List in java
// using for loop
// Importing all input output classes
import java.io.*;
// Importing all utility classes from
// java.util package
import java.util.*;
 
// Class
class GFG {
 
    // main driver method
    public static void main(String[] args)
    {
        // Creating an ArrayList object
        // Declaring object of Integer type
        // Custom entries in array
        List my_list
            = Arrays.asList(10, 20, 30, 40, 50);
 
        // Display message
        System.out.print("Iterating over ArrayList: ");
 
        // Iteration over ArrayList
        // using the for loop
        for (int i = 0; i < my_list.size(); i++)
 
            // Print and display the all elements
            // in List object
            System.out.print(my_list.get(i) + " ");
 
        // new line
        System.out.println();
 
        // No, creating a vector of size N
        // Custom entry for N = 5
        // Custom Integer entries
        List v = new Vector(5);
        v.add(10);
        v.add(20);
        v.add(30);
        v.add(40);
        v.add(50);
 
        // Display message
        System.out.print("Iterating over Vector:    ");
 
        // iterating over vector using for loop
        for (int i = 0; i < v.size(); i++)
 
            // Print and display vector elements
            System.out.print(v.get(i) + " ");
 
        // New Line
        System.out.println();
 
        // Creating a stack containing Integer elements
        List s = new Stack();
 
        // Adding integer elements
        // Custom input
        s.add(10);
        s.add(20);
        s.add(30);
        s.add(40);
        s.add(50);
 
        // Display message
        System.out.print("Iterating over Stack:     ");
 
        // For loop o iterate over elements in stack
        for (int i = 0; i < v.size(); i++)
 
            // Print and display all stack elements
            System.out.print(s.get(i) + " ");
    }
}


Java
// Java Program to iterate over List
// using while loop
 
// Importing all input output classes
import java.io.*;
// Importing all utility classes from
// java.util package
import java.util.*;
 
// Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of List
        // Declaring object of Integer type
        // Custom Integer entries
        List my_list
            = Arrays.asList(10, 20, 30, 40, 50);
 
        // Display message
        System.out.print("Iterating over ArrayList: ");
 
        // Initially loop variable is initialized
        // with zero
        int i = 0;
 
        // Iterating over List via while loop
        // using size() method
        while (i < my_list.size()) {
 
            // Print and display all elements
            // of an ArrayList
            System.out.print(my_list.get(i) + " ");
 
            // Incrementing the counter by unity safter
            // one iteration
            i++;
        }
 
        i = 0;
 
        // New Line
        System.out.println();
 
        // Creating a Vector of size N
        // Custom value for N = 5
        List v = new Vector(5);
 
        // Adding 5 elements to the above List object
        // for vector
        // Custom entries
        v.add(10);
        v.add(20);
        v.add(30);
        v.add(40);
        v.add(50);
 
        // Display message
        System.out.print("Iterating over Vector:    ");
 
        // Iterating over Vector via while loop
        // using the size() method
        while (i < v.size()) {
 
            // Print and display all elements of vector
            System.out.print(v.get(i) + " ");
 
            // Increment the counter variable
            i++;
        }
 
        //  Counter variable is initially
        // initialized with zero
        i = 0;
 
        // New Line
        System.out.println();
 
        // Creating a Stack by creating another
        // list object of Integer type
        // Declaring object of Integer type
        List s = new Stack();
 
        // Adding elements to the above stack
        // Custom entries
        s.add(10);
        s.add(20);
        s.add(30);
        s.add(40);
        s.add(50);
 
        // Display message
        System.out.print("Iterating over Stack:     ");
 
        // Iterating over stack via while loop
        // using size method()
        while (i < v.size()) {
 
            // Print and display all elements
            // of the above stack/ obj created
            System.out.print(s.get(i) + " ");
 
            // Increment the counter by unity
            i++;
        }
    }
}


Java
/*package whatever //do not write package name here */
 
import java.io.*;
import java.util.*;
class GFG {
    public static void main(String[] args)
    {
        // creating Arraylist
        List my_list
            = Arrays.asList(10, 20, 30, 40, 50);
 
      
        System.out.print("Iterating over ArrayList: ");
         // For Each Loop for iterating ArrayList
        for (Integer i :my_list)
            System.out.print(i + " ");
 
        System.out.println();
 
        // creating Vector of size 5
        List v = new Vector(5);
        v.add(10);
        v.add(20);
        v.add(30);
        v.add(40);
        v.add(50);
 
        System.out.print("Iterating over Vector:    ");
         // For Each Loop for iterating Vector
        for (Integer i : v)
            System.out.print(i + " ");
 
        System.out.println();
 
        // creating Stack
        List s = new Stack();
        s.add(10);
        s.add(20);
        s.add(30);
        s.add(40);
        s.add(50);
 
        
        System.out.print("Iterating over Stack:     ");
        // For Each Loop for iterating Stack
        for (Integer i : s)
            System.out.print(i + " ");
    }
}


Java
// Importing all input output classes
import java.io.*;
// Importing all classes from
// java,util package
import java.util.*;
 
// Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an Arraylist by creating object
        // of List and declaring as Integer type
        // Custom  Integer entries
        List my_list
            = Arrays.asList(10, 20, 30, 40, 50);
 
        // Display message
        System.out.print("Iterating over ArrayList: ");
 
        // Traversing over ArrayList
        // using for each method Java 8
        my_list.forEach(
            list -> System.out.print(list + " "));
 
        // New line
        System.out.println();
 
        // creating Vector by creating object of
        // List and declaring as Integer type
 
        // Vector is of size N
        // N = 5 for illustration purposes
        List v = new Vector(5);
 
        // Adding elements to the vector
        // Custom Integer elements
        v.add(10);
        v.add(20);
        v.add(30);
        v.add(40);
        v.add(50);
 
        // Display message
        System.out.print("Iterating over Vector:    ");
 
        // Traversing the above vector elements
        // using for each method Java 8
        v.forEach(vector -> System.out.print(vector + " "));
 
        // New line
        System.out.println();
 
        // Creating a Stack by creating an object of
        // List and declaring it as of Integer type
        List s = new Stack();
 
        // Adding elements to the above stack created
        // Custom inputs addition using add() method
        s.add(10);
        s.add(20);
        s.add(30);
        s.add(40);
        s.add(50);
 
        // Display message
        System.out.print("Iterating over Stack:     ");
 
        // Print and display all the elements inside stack
        // using for each method Java 8
        s.forEach(stack -> System.out.print(stack + " "));
    }
}



输出
Iterating over ArrayList: 10 20 30 40 50 
Iterating over Vector:    10 20 30 40 50 
Iterating over Stack:     10 20 30 40 50 

方法二:使用While循环

Java while 循环类似于 For 循环,是一种控制流语句,它允许代码重复运行,直到满足所需条件。

例子

Java

// Java Program to iterate over List
// using while loop
 
// Importing all input output classes
import java.io.*;
// Importing all utility classes from
// java.util package
import java.util.*;
 
// Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of List
        // Declaring object of Integer type
        // Custom Integer entries
        List my_list
            = Arrays.asList(10, 20, 30, 40, 50);
 
        // Display message
        System.out.print("Iterating over ArrayList: ");
 
        // Initially loop variable is initialized
        // with zero
        int i = 0;
 
        // Iterating over List via while loop
        // using size() method
        while (i < my_list.size()) {
 
            // Print and display all elements
            // of an ArrayList
            System.out.print(my_list.get(i) + " ");
 
            // Incrementing the counter by unity safter
            // one iteration
            i++;
        }
 
        i = 0;
 
        // New Line
        System.out.println();
 
        // Creating a Vector of size N
        // Custom value for N = 5
        List v = new Vector(5);
 
        // Adding 5 elements to the above List object
        // for vector
        // Custom entries
        v.add(10);
        v.add(20);
        v.add(30);
        v.add(40);
        v.add(50);
 
        // Display message
        System.out.print("Iterating over Vector:    ");
 
        // Iterating over Vector via while loop
        // using the size() method
        while (i < v.size()) {
 
            // Print and display all elements of vector
            System.out.print(v.get(i) + " ");
 
            // Increment the counter variable
            i++;
        }
 
        //  Counter variable is initially
        // initialized with zero
        i = 0;
 
        // New Line
        System.out.println();
 
        // Creating a Stack by creating another
        // list object of Integer type
        // Declaring object of Integer type
        List s = new Stack();
 
        // Adding elements to the above stack
        // Custom entries
        s.add(10);
        s.add(20);
        s.add(30);
        s.add(40);
        s.add(50);
 
        // Display message
        System.out.print("Iterating over Stack:     ");
 
        // Iterating over stack via while loop
        // using size method()
        while (i < v.size()) {
 
            // Print and display all elements
            // of the above stack/ obj created
            System.out.print(s.get(i) + " ");
 
            // Increment the counter by unity
            i++;
        }
    }
}


输出
Iterating over ArrayList: 10 20 30 40 50 
Iterating over Vector:    10 20 30 40 50 
Iterating over Stack:     10 20 30 40 50 

方法 3:使用 for 每个循环

句法:

for (type temp : list_name) 
{  
   statements using temp;
}

例子

Java

/*package whatever //do not write package name here */
 
import java.io.*;
import java.util.*;
class GFG {
    public static void main(String[] args)
    {
        // creating Arraylist
        List my_list
            = Arrays.asList(10, 20, 30, 40, 50);
 
      
        System.out.print("Iterating over ArrayList: ");
         // For Each Loop for iterating ArrayList
        for (Integer i :my_list)
            System.out.print(i + " ");
 
        System.out.println();
 
        // creating Vector of size 5
        List v = new Vector(5);
        v.add(10);
        v.add(20);
        v.add(30);
        v.add(40);
        v.add(50);
 
        System.out.print("Iterating over Vector:    ");
         // For Each Loop for iterating Vector
        for (Integer i : v)
            System.out.print(i + " ");
 
        System.out.println();
 
        // creating Stack
        List s = new Stack();
        s.add(10);
        s.add(20);
        s.add(30);
        s.add(40);
        s.add(50);
 
        
        System.out.print("Iterating over Stack:     ");
        // For Each Loop for iterating Stack
        for (Integer i : s)
            System.out.print(i + " ");
    }
}


输出
Iterating over ArrayList: 10 20 30 40 50 
Iterating over Vector:    10 20 30 40 50 
Iterating over Stack:     10 20 30 40 50 

方法四:在Java 8的每个循环中使用

此方法将函数接口作为参数,因此可以将 lambda 表达式作为参数传递。

句法:

void forEach(Consumer action)

例子

Java

// Importing all input output classes
import java.io.*;
// Importing all classes from
// java,util package
import java.util.*;
 
// Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an Arraylist by creating object
        // of List and declaring as Integer type
        // Custom  Integer entries
        List my_list
            = Arrays.asList(10, 20, 30, 40, 50);
 
        // Display message
        System.out.print("Iterating over ArrayList: ");
 
        // Traversing over ArrayList
        // using for each method Java 8
        my_list.forEach(
            list -> System.out.print(list + " "));
 
        // New line
        System.out.println();
 
        // creating Vector by creating object of
        // List and declaring as Integer type
 
        // Vector is of size N
        // N = 5 for illustration purposes
        List v = new Vector(5);
 
        // Adding elements to the vector
        // Custom Integer elements
        v.add(10);
        v.add(20);
        v.add(30);
        v.add(40);
        v.add(50);
 
        // Display message
        System.out.print("Iterating over Vector:    ");
 
        // Traversing the above vector elements
        // using for each method Java 8
        v.forEach(vector -> System.out.print(vector + " "));
 
        // New line
        System.out.println();
 
        // Creating a Stack by creating an object of
        // List and declaring it as of Integer type
        List s = new Stack();
 
        // Adding elements to the above stack created
        // Custom inputs addition using add() method
        s.add(10);
        s.add(20);
        s.add(30);
        s.add(40);
        s.add(50);
 
        // Display message
        System.out.print("Iterating over Stack:     ");
 
        // Print and display all the elements inside stack
        // using for each method Java 8
        s.forEach(stack -> System.out.print(stack + " "));
    }
}


输出
Iterating over ArrayList: 10 20 30 40 50 
Iterating over Vector:    10 20 30 40 50 
Iterating over Stack:     10 20 30 40 50