我是靠谱客的博主 羞涩龙猫,这篇文章主要介绍java中的Iterator迭代器,现在分享给大家,希望可以做个参考。

Iterable接口是Java集合的顶级接口之一。Collection接口继承Iterable,所以Collection的所有子类也实现了Iterable接口。
在API中查看该接口:
在这里插入图片
在这里插入图片描述
实现了这个接口可以获得增强for循环的操作
再往下发现该接口只有一个方法:iterator
在这里插入图片描述
该方法的返回值是一个名为Iterator的接口,这个接口也被称为迭代器。
在API中查看这个接口的内容:
在这里插入图片描述
在这里插入图片描述

在这个接口下有三个方法:
在这里插入图片描述
方法hasNext():在迭代器底层其实是有一个指针指向容器内的元素,在未调用hasNext()之前,指针指向的是容器第一个元素的前一个位置。调用hasNext()方法时,如果该指针指向元素的下一个位置有元素存在, 则返回True,否则返回False。

方法next():返回指针指向的元素,并将指针下下移一个单位。
注意:如果指针指向最后一个元素,此时再调用next()会抛出异常NoSuchElementException

方法remove():从容器中移除元素。只有在调用next()后才能调用一次remove,调用后会把上面next返回的元素移除容器。
在这里插入图片描述

在这里插入图片描述

注意:迭代器在执行过程中不能对容器内的元素做增删操作(合法调用迭代器自带的remove除外),这样会使迭代器不合法,发生不可预知的后果。

对容器进行遍历有三种方式:
①普通for循环:通过下标获取元素
②使用迭代器:容器必须实现Iterator接口,在迭代器遍历过程中不能使用除迭代器自带的remove()方法对迭代器进行增删。
③增强for循环:底层使用迭代器实现,本质就是Iterator迭代器,所有在遍历过程中不能对容器进行增删操作。

示例:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class test01 { public static void main(String[] args) { ArrayList array = new ArrayList(); array.add(1); array.add(2); array.add(3); System.out.println(array); // [1, 2, 3] //遍历容器的三种方法: //方法1:普通for循环(根据下标获取元素) System.out.println("========普通for循环========"); for (int i = 0; i < array.size() ; i++) { System.out.println(array.get(i)); } // 方法2:通过迭代器 System.out.println("===================迭代器======================"); Iterator iterator = array.iterator(); //调用iterator方法返回一个迭代器 while (iterator.hasNext()) { Integer next = (Integer) iterator.next(); System.out.println(next); } //方法3:增强for循环 System.out.println("===========增强for循环================"); for (Object i : array) { System.out.println(i); } // 方法2:remove() System.out.println("===================迭代器remove()======================"); Iterator iterator1 = array.iterator(); //调用iterator方法返回一个迭代器 while (iterator1.hasNext()) { Integer next = (Integer) iterator1.next(); if (next.equals(2)) { iterator1.remove(); } System.out.println(next); } System.out.println(array); //[1, 3] } }

运行结果:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[1, 2, 3] ========普通for循环======== 1 2 3 ===================迭代器====================== 1 2 3 ===========增强for循环================ 1 2 3 ===================迭代器====================== 1 2 3 [1, 3]

扩展1:利用迭代器对容器元素进行修改

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class test02 { public static void main(String[] args) { ArrayList arr = new ArrayList(); people p1 = new people("关羽", 10); people p2 = new people("张飞", 11); arr.add(p1); arr.add(p2); System.out.println(arr); Iterator iterator = arr.iterator(); while (iterator.hasNext()) { people next = (people) iterator.next(); if (next.name.equals("关羽")) { next.name = "刘备"; } System.out.println(next); } System.out.println(arr); } } class people{ String name; int age; public people(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "people{" + "name='" + name + ''' + ", age=" + age + '}'; } }

结果:

复制代码
1
2
3
4
5
[people{name='关羽', age=10}, people{name='张飞', age=11}] people{name='刘备', age=10} people{name='张飞', age=11} [people{name='刘备', age=10}, people{name='张飞', age=11}]

扩展2:增强for循环与迭代器

复制代码
1
2
3
4
5
6
//方法3:增强for循环 System.out.println("===========增强for循环================"); for (Object i : array) { System.out.println(i); }

对上面代码进行反编译:

复制代码
1
2
3
4
5
6
System.out.println("===========增强for循环================"); for (Integer next = array.iterator(); next.hasNext(); ) { Object i = next.next(); System.out.println(i); }

可以看到增强for循环底层就是利用迭代器实现的…

最后

以上就是羞涩龙猫最近收集整理的关于java中的Iterator迭代器的全部内容,更多相关java中内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(69)

评论列表共有 0 条评论

立即
投稿
返回
顶部