在Java中迭代 ArrayList
ArrayList 是集合框架的一部分,存在于Java.util 包中。它为我们提供了Java中的动态数组。虽然,它可能比标准数组慢,但在需要对数组进行大量操作的程序中很有帮助。此类位于Java.util包中。
随着Java版本的引入和升级,新的方法正在可用,就像我们从 Java8 中看到的感知 lambda 表达式和流概念在它之前不可用,因为它是在Java version8 中引入的。
方法:
- 使用 for 循环
- 使用同时
- 使用 for-each 循环
- 使用迭代器
- 使用 Lambda 表达式(仅限 Java8 之后)
- 使用枚举接口
让我们讨论一下这些方法,我们可以立即认为开始的三种方法只是简单的方法,而进一步的方法对它们进行了一些优化。请记住,虽然遍历元素较少,但我们通常倾向于通过幼稚的方法进行迭代,否则如果要插入的元素的大小很大,那么我们确实使用最佳方法。让我们快速包装上述每种方法。
方法一:使用for循环
Java
// Java program to iterate over an ArrayList
// Using for loop
// Importing all utility classes
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating and initializing the ArrayList
// Declaring object of integer type
List numbers = Arrays.asList(1, 2, 3,
4, 5, 6, 7, 8);
// Iterating using for loop
for (int i = 0; i < numbers.size(); i++)
// Printing and display the elements in ArrayList
System.out.print(numbers.get(i) + " ");
}
}
Java
// Java Program to Illustrate ArrayList
// Using While Loop
// Importing required classes
import java.util.ArrayList ;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating and initializing the ArrayList
// Declaring object of integer type
ArrayList al = new ArrayList();
// Adding elements to ArrayList
// using add() method
al.add(3);
al.add(1);
al.add(7);
al.add(20);
al.add(5);
// Step 1: Setting and initializing a variable
// as per syntax of while loop
// Initially declaring and setting
int val = 0;
// Step 2: Condition
// Till our counter variable is lesser than size of
// ArrayList
while (al.size() > val) {
// Printing the element which holds above
// condition true
System.out.println(al.get(val));
// Step 3: Terminating condition by incrementing
// our counter in each iteration
val++ ;
}
}
}
Java
// Java Program to Iterate over Arraylist
// using for Each loop
// Importing all utility classes
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing ArrayList
List numbers
= Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
// For Each Loop for iterating ArrayList
for (Integer i : numbers)
// Printing the elements of ArrayList
System.out.print(i + " ");
}
}
Java
// Java program to iterate over an ArrayList
// Using Iterator
// Importing all utility classes
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing ArrayList
List numbers
= Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
// Iterating ArrayList using Iterator
Iterator it = numbers.iterator();
// Holds true till there is single element
// remaining in the list
while (it.hasNext())
// Print the elements of ArrayList
System.out.print(it.next() + " ");
}
}
Java
// Java program to iterate over an arraylist
// using Iterator in Java8 with Lambda Expression
// Importing all utility classes
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing ArrayList
// Custom input elements
List numbers = Arrays.asList(1, 2, 3,
4, 5, 6, 7, 8);
// Printing numbers using lambda expressions
// been introduced later in java8
numbers.forEach(number->System.out.println(number));
}
}
Java
// Java Program to Iterate over ArrayList elements
// Using Enumeration
// Importing required classes
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an ArrayList
ArrayList al = new ArrayList();
// Adding elements to ArrayList
al.add(34);
al.add(12);
al.add(34);
al.add(23);
al.add(54);
// Getting an enumeration object
Enumeration e
= Collections.enumeration(al);
// Till elements are there
while (e.hasMoreElements())
// Print elements using nextElement() method
System.out.println(e.nextElement());
}
}
Java
// Java program to demonstrate Working of
// Iterator.remove() on Arraylist
// Importing utility classes
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
// Main class
public class GFG
{
// Main driver method
public static void main(String[] args)
{
// Creating a List with referenceto ArrayList
List al = new ArrayList();
al.add(10);
al.add(20);
al.add(30);
al.add(1);
al.add(2);
// Remove elements smaller than 10 using
// Iterator.remove()
Iterator itr = al.iterator();
while (itr.hasNext())
{
int x = (Integer)itr.next();
if (x < 10)
itr.remove();
}
System.out.println("Modified ArrayList : "
+ al);
}
}
输出
1 2 3 4 5 6 7 8
方法二:使用while循环
Java
// Java Program to Illustrate ArrayList
// Using While Loop
// Importing required classes
import java.util.ArrayList ;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating and initializing the ArrayList
// Declaring object of integer type
ArrayList al = new ArrayList();
// Adding elements to ArrayList
// using add() method
al.add(3);
al.add(1);
al.add(7);
al.add(20);
al.add(5);
// Step 1: Setting and initializing a variable
// as per syntax of while loop
// Initially declaring and setting
int val = 0;
// Step 2: Condition
// Till our counter variable is lesser than size of
// ArrayList
while (al.size() > val) {
// Printing the element which holds above
// condition true
System.out.println(al.get(val));
// Step 3: Terminating condition by incrementing
// our counter in each iteration
val++ ;
}
}
}
输出
3
1
7
20
5
方法3:使用for每个循环
Java
// Java Program to Iterate over Arraylist
// using for Each loop
// Importing all utility classes
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing ArrayList
List numbers
= Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
// For Each Loop for iterating ArrayList
for (Integer i : numbers)
// Printing the elements of ArrayList
System.out.print(i + " ");
}
}
输出
1 2 3 4 5 6 7 8
方法四:使用迭代器
Java
// Java program to iterate over an ArrayList
// Using Iterator
// Importing all utility classes
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing ArrayList
List numbers
= Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
// Iterating ArrayList using Iterator
Iterator it = numbers.iterator();
// Holds true till there is single element
// remaining in the list
while (it.hasNext())
// Print the elements of ArrayList
System.out.print(it.next() + " ");
}
}
输出
1 2 3 4 5 6 7 8
方法 5:使用 Lambda 表达式
Java
// Java program to iterate over an arraylist
// using Iterator in Java8 with Lambda Expression
// Importing all utility classes
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing ArrayList
// Custom input elements
List numbers = Arrays.asList(1, 2, 3,
4, 5, 6, 7, 8);
// Printing numbers using lambda expressions
// been introduced later in java8
numbers.forEach(number->System.out.println(number));
}
}
输出
1
2
3
4
5
6
7
8
方法六:使用枚举接口
Java
// Java Program to Iterate over ArrayList elements
// Using Enumeration
// Importing required classes
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an ArrayList
ArrayList al = new ArrayList();
// Adding elements to ArrayList
al.add(34);
al.add(12);
al.add(34);
al.add(23);
al.add(54);
// Getting an enumeration object
Enumeration e
= Collections.enumeration(al);
// Till elements are there
while (e.hasMoreElements())
// Print elements using nextElement() method
System.out.println(e.nextElement());
}
}
输出
34
12
34
23
54
现在它是本文的进一步补充,因为我们已经讨论了所有可用于迭代元素的方法。到目前为止,我们只遍历了输入元素,还没有看到遍历如果我们玩元素会怎样,所以我们正在考虑
例子
Java
// Java program to demonstrate Working of
// Iterator.remove() on Arraylist
// Importing utility classes
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
// Main class
public class GFG
{
// Main driver method
public static void main(String[] args)
{
// Creating a List with referenceto ArrayList
List al = new ArrayList();
al.add(10);
al.add(20);
al.add(30);
al.add(1);
al.add(2);
// Remove elements smaller than 10 using
// Iterator.remove()
Iterator itr = al.iterator();
while (itr.hasNext())
{
int x = (Integer)itr.next();
if (x < 10)
itr.remove();
}
System.out.println("Modified ArrayList : "
+ al);
}
}
输出
Modified ArrayList : [10, 20, 30]
在遍历期间删除项目:不建议在遍历元素时使用 ArrayList.remove()。这可能会导致 ConcurrentModificationException(有关此异常的示例程序,请参阅this)。迭代元素时,推荐使用 Iterator.remove() 方法。