在Java中遍历 List
Java中的列表允许我们维护一个有序的对象集合。在Java中,重复元素和空元素也可以存储在 List 中。 List 接口是Java.util包的一部分,它继承了 Collection 接口。它保留了插入的顺序。
有几种方法可以在Java中迭代 List。它们将在下面讨论:
方法:
- 使用循环(朴素方法)
- For 循环
- For-each 循环
- while 循环
- 使用迭代器
- 使用列表迭代器
- 使用 lambda 表达式
- 使用 stream.forEach()
方法 1-A:简单的 for 循环
可以使用简单的 for 循环通过迭代访问每个元素。可以使用索引作为循环变量来访问索引。
句法:
for (i = 0; i < list_name.size(); i++)
{
// code block to be executed
}
例子
Java
// Java Program to iterate over List
// Using simple for loop
// Importing all classes of
// java.util package
import java.util.*;
// CLass
class GFG {
// Main driver method
public static void main(String args[])
{
// Creating a ArrayList
List myList = new ArrayList();
// Adding elements to the list
// Custom innputs
myList.add("A");
myList.add("B");
myList.add("C");
myList.add("D");
// For loop for iterating over the List
for (int i = 0; i < myList.size(); i++) {
// Print all elements of List
System.out.println(myList.get(i));
}
}
}
Java
// Java Program to Iterate over a List
// using enhanced for loop (for-each)
// Importing all classes of
// java.util package
import java.util.*;
// Class
class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an Arraylist
List myList = new ArrayList();
// Adding elements to the List
// Custom inputs
myList.add("A");
myList.add("B");
myList.add("C");
myList.add("D");
// Using enhanced for loop(for-each) for iteration
for (String i : myList) {
// Print all elements of ArrayList
System.out.println(i);
}
}
}
Java
// Java Program to iterate over a List
// using while loop
// Importing all classess of
// java.util package
import java.util.*;
// Class
class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an ArrayList
List myList = new ArrayList();
// Adding elements to the List
// Custom inputs
myList.add("A");
myList.add("B");
myList.add("C");
myList.add("D");
// Initializing any variable to 0
int i = 0;
// If variable value is lesser than
// value indicating size of List
while (i < myList.size()) {
// Print element of list
System.out.println(myList.get(i));
// Increase variable count by 1
i++;
}
}
}
Java
// Java Program to iterate over the list
// using iterator
// Importing all classes of
// java.util package
import java.util.*;
// Class
class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an ArrayList
List myList = new ArrayList();
// Adding elements to the List
// Custom inputs
myList.add("A");
myList.add("B");
myList.add("C");
myList.add("D");
// Iterator
Iterator it = myList.iterator();
// Condition check for elements in List
// using hasNext() method returning true till
// there i single element in a List
while (it.hasNext()) {
// Print all elements of List
System.out.println(it.next());
}
}
}
Java
// Java program to iterate over a list
// using ListIterator
import java.util.*;
// Class
class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an ArrayList
List myList = new ArrayList();
// Adding elements to the List
// Custom inputs
myList.add("A");
myList.add("B");
myList.add("C");
myList.add("D");
// List iterator
ListIterator it = myList.listIterator();
// Condition check whether there is element in List
// using hasNext() which holds true till
// there is single element in List
while (it.hasNext()) {
// Print all elements of List
System.out.println(it.next());
}
}
}
Java
// Java Program to iterate over a List
// using forEach()
// Importing all classes of
// java.util method
import java.util.*;
// Class
class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an ArrayList
List myList = new ArrayList();
// Adding elements to the List
// Custom inputs
myList.add("A");
myList.add("B");
myList.add("C");
myList.add("D");
// Lambda expression printing all elements in a List
myList.forEach(
(temp) -> { System.out.println(temp); });
}
}
Java
// Java Program iterating over a List
// using stream.forEach() method
// Importing all classes of
// java.util method
import java.util.*;
// Class
class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an ArrayList
List myList = new ArrayList();
// Adding elements to the List
// Custom inputs
myList.add("A");
myList.add("B");
myList.add("C");
myList.add("D");
// stream.forEach() method prints
// all elements inside a List
myList.stream().forEach(
(temp) -> System.out.println(temp));
}
}
A
B
C
D
方法 1-B:增强的 for 循环
可以使用增强的 for 循环通过迭代访问每个元素。这个循环是在 J2SE 5.0 中引入的。这是遍历 for 循环的另一种方法。它使代码更具可读性。
句法:
for(data_type variable : List_name)
{
// Body of the loop.
// Each element can be accessed using variable.
}
Java
// Java Program to Iterate over a List
// using enhanced for loop (for-each)
// Importing all classes of
// java.util package
import java.util.*;
// Class
class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an Arraylist
List myList = new ArrayList();
// Adding elements to the List
// Custom inputs
myList.add("A");
myList.add("B");
myList.add("C");
myList.add("D");
// Using enhanced for loop(for-each) for iteration
for (String i : myList) {
// Print all elements of ArrayList
System.out.println(i);
}
}
}
A
B
C
D
方法 1-C:使用 while 循环
也可以使用 while 循环来实现对列表的迭代。循环内的代码块会一直执行,直到条件为真。循环变量可用作访问每个元素的索引。
句法:
while(variable
Java
// Java Program to iterate over a List
// using while loop
// Importing all classess of
// java.util package
import java.util.*;
// Class
class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an ArrayList
List myList = new ArrayList();
// Adding elements to the List
// Custom inputs
myList.add("A");
myList.add("B");
myList.add("C");
myList.add("D");
// Initializing any variable to 0
int i = 0;
// If variable value is lesser than
// value indicating size of List
while (i < myList.size()) {
// Print element of list
System.out.println(myList.get(i));
// Increase variable count by 1
i++;
}
}
}
A
B
C
D
方法二:使用迭代器
迭代器是Java中的一个对象,它允许迭代集合的元素。列表中的每个元素都可以使用带有 while 循环的迭代器访问。
句法:
Iterator variable = list_name.iterator();
例子
Java
// Java Program to iterate over the list
// using iterator
// Importing all classes of
// java.util package
import java.util.*;
// Class
class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an ArrayList
List myList = new ArrayList();
// Adding elements to the List
// Custom inputs
myList.add("A");
myList.add("B");
myList.add("C");
myList.add("D");
// Iterator
Iterator it = myList.iterator();
// Condition check for elements in List
// using hasNext() method returning true till
// there i single element in a List
while (it.hasNext()) {
// Print all elements of List
System.out.println(it.next());
}
}
}
A
B
C
D
方法 3:使用列表迭代器
ListIterator 是一个迭代器,它是一个从 1.2 版本开始可用的Java 。它允许我们从 List 实现的对象中一个一个地迭代元素。它用于使用 while 循环遍历列表。
句法
ListIterator variable = list_name.listIterator();
Java
// Java program to iterate over a list
// using ListIterator
import java.util.*;
// Class
class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an ArrayList
List myList = new ArrayList();
// Adding elements to the List
// Custom inputs
myList.add("A");
myList.add("B");
myList.add("C");
myList.add("D");
// List iterator
ListIterator it = myList.listIterator();
// Condition check whether there is element in List
// using hasNext() which holds true till
// there is single element in List
while (it.hasNext()) {
// Print all elements of List
System.out.println(it.next());
}
}
}
A
B
C
D
方法 4:使用 Iterable.forEach()
此功能从Java 8 开始可用。它还可以用于迭代 List。迭代可以使用lambda 表达式完成。
句法:
list_name.forEach(variable->{//block of code})
Java
// Java Program to iterate over a List
// using forEach()
// Importing all classes of
// java.util method
import java.util.*;
// Class
class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an ArrayList
List myList = new ArrayList();
// Adding elements to the List
// Custom inputs
myList.add("A");
myList.add("B");
myList.add("C");
myList.add("D");
// Lambda expression printing all elements in a List
myList.forEach(
(temp) -> { System.out.println(temp); });
}
}
A
B
C
D
方法 5:使用 Stream.forEach()
stream().forEach() 的处理顺序是未定义的,而在 forEach() 的情况下,它是定义的。两者都可用于迭代 List。
句法:
list_name.stream.forEach(variable->{//block of code})
Java
// Java Program iterating over a List
// using stream.forEach() method
// Importing all classes of
// java.util method
import java.util.*;
// Class
class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an ArrayList
List myList = new ArrayList();
// Adding elements to the List
// Custom inputs
myList.add("A");
myList.add("B");
myList.add("C");
myList.add("D");
// stream.forEach() method prints
// all elements inside a List
myList.stream().forEach(
(temp) -> System.out.println(temp));
}
}
A
B
C
D