Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么要使用';迭代器';循环通过ArrayList。不使用迭代器也可以完成,对吗?_Java_Arraylist - Fatal编程技术网

Java 为什么要使用';迭代器';循环通过ArrayList。不使用迭代器也可以完成,对吗?

Java 为什么要使用';迭代器';循环通过ArrayList。不使用迭代器也可以完成,对吗?,java,arraylist,Java,Arraylist,为什么要使用“迭代器”在ArrayList中循环。不使用迭代器也可以完成,对吗? 因此,使用迭代器循环遍历ArrayList的好处是 import java.util.ArrayList; import java.util.Iterator; public class IteratorDemo { public static void main(String[] args) { // Make a collection ArrayList<String> c

为什么要使用“迭代器”在ArrayList中循环。不使用迭代器也可以完成,对吗? 因此,使用迭代器循环遍历ArrayList的好处是

import java.util.ArrayList;
import java.util.Iterator;

 public class IteratorDemo {
  public static void main(String[] args) {

    // Make a collection
    ArrayList<String> cars = new ArrayList<String>();
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");

    // Get the iterator
    //Iterator<String> it = cars.iterator();

    // Looping without Iterator
    for(int i=0;i<4;i++) {
        //System.out.println(it.next());

        System.out.println(cars.get(i));
    }
  }
 }
import java.util.ArrayList;
导入java.util.Iterator;
公共类迭代器demo{
公共静态void main(字符串[]args){
//收藏
ArrayList cars=新的ArrayList();
添加(“沃尔沃”);
加上(“宝马”);
汽车。添加(“福特”);
添加(“马自达”);
//获取迭代器
//迭代器it=cars.Iterator();
//无迭代器循环

对于(int i=0;i有时您不需要索引,因此更简单。但请尝试使用索引for循环来执行此操作。可以这样做,但不那么容易

List<Integer> list = new ArrayList<>(
                List.of(1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10));
Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
   // if even, remove it.
   if (it.next() % 2 == 0) {
      it.remove();
   }
}
System.out.println(list);
使用
interator
可以修改可能触发并发修改异常的列表

另外,假设您有一个生成素数或斐波那契数的类实现,您可以这样做来单独获取它们

Fibonacci fib = new Fibonacci(0, 1, 10);
for (long i : fib) {
    System.out.print(i + " ");
}
印刷品

[1, 3, 5, 7, 9]
0 1 1 2 3 5 8 13 21 34 
斐波那契类

class Fibonacci implements Iterable<Long> {
    public long start;
    public long next;
    public long count;
    
    public Fibonacci(int start, int next, int count) {
        this.start = start;
        this.next = next;
        this.count = count;
    }
    
    private class MyIterator implements Iterator<Long> {
        int n = 0;
        
        public boolean hasNext() {
            return n < count;
        }
        
        public Long next() {
            n++;
            long ret = start;
            start = start + next;
            next = ret;
            return ret;
        }
    }
    
    public Iterator<Long> iterator() {
        return new MyIterator();
    }
}
类Fibonacci实现了Iterable{
公众长期启动;
公众长下;
公众长时间计数;
公共斐波那契(整数开始、整数下一步、整数计数){
this.start=start;
this.next=next;
this.count=计数;
}
私有类MyIterator实现了迭代器{
int n=0;
公共布尔hasNext(){
返回n

通过在类中实现
Iterable
,我可以使用
foreach
构造来获取类的元素。

有时您不需要索引,因此更容易。但是尝试使用索引for循环来实现这一点。这可以做到,但不是那么容易

List<Integer> list = new ArrayList<>(
                List.of(1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10));
Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
   // if even, remove it.
   if (it.next() % 2 == 0) {
      it.remove();
   }
}
System.out.println(list);
使用
interator
可以修改可能触发并发修改异常的列表

另外,假设您有一个生成素数或斐波那契数的类实现,您可以这样做来单独获取它们

Fibonacci fib = new Fibonacci(0, 1, 10);
for (long i : fib) {
    System.out.print(i + " ");
}
印刷品

[1, 3, 5, 7, 9]
0 1 1 2 3 5 8 13 21 34 
斐波那契类

class Fibonacci implements Iterable<Long> {
    public long start;
    public long next;
    public long count;
    
    public Fibonacci(int start, int next, int count) {
        this.start = start;
        this.next = next;
        this.count = count;
    }
    
    private class MyIterator implements Iterator<Long> {
        int n = 0;
        
        public boolean hasNext() {
            return n < count;
        }
        
        public Long next() {
            n++;
            long ret = start;
            start = start + next;
            next = ret;
            return ret;
        }
    }
    
    public Iterator<Long> iterator() {
        return new MyIterator();
    }
}
类Fibonacci实现了Iterable{
公众长期启动;
公众长下;
公众长时间计数;
公共斐波那契(整数开始、整数下一步、整数计数){
this.start=start;
this.next=next;
this.count=计数;
}
私有类MyIterator实现了迭代器{
int n=0;
公共布尔hasNext(){
返回n

通过在类中实现
Iterable
,我可以使用
foreach
构造来获取类的元素。

您可能没有直接使用它的好处。但是迭代器是由foreach使用的。

您可能没有直接使用它的好处。但是迭代器是由例如for使用的每个。

只要循环通过它,您不需要迭代器,for循环就足够了。如果您需要修改列表,那么您需要它来避免ConcurrentModificationException。

只要循环通过它,您不需要迭代器,for循环就足够了。如果您需要修改列表,那么您需要它来避免ConcurrentModificationExceptionn、

我以为问题是“为什么使用迭代器?”。所以我写的是对这个问题的回答。不是评论。也许我当时误解了这个问题。我以为问题是“为什么使用迭代器?”。所以我写的是对这个问题的回答。不是评论。也许我当时误解了这个问题。