迭代器--遍历集合的专用工具

【不使用迭代器遍历集合】

 1 package com.hxl;
 2
 3 import java.util.ArrayList;
 4 import java.util.Arrays;
 5 import java.util.Collection;
 6
 7 public class Test {
 8
 9     public static void main(String[] args) {
10         // 实例化一个集合对象
11         Collection c = new ArrayList<>();
12         // 添加元素
13         c.add(new Student("aa", 11, ‘男‘));
14         c.add(new Student("bb", 22, ‘男‘));
15         c.add(new Student("cc", 33, ‘女‘));
16         c.add(new Student("dd", 44, ‘男‘));
17         c.add(new Student("ee", 55, ‘女‘));
18         // 将集合转为Object[]数组
19         Object[] objs = c.toArray();
20         // 遍历数组
21         for (int i = 0; i < objs.length; i++) {
22             // 这里Student类中的toString()方法已经重写过了
23             System.out.println(objs[i]);
24         }
25     }
26 }

【使用迭代器Iterator】

 1 package com.hxl;
 2
 3 import java.util.ArrayList;
 4 import java.util.Arrays;
 5 import java.util.Collection;
 6 import java.util.Iterator;
 7
 8 public class Test {
 9
10     public static void main(String[] args) {
11         // 实例化一个集合对象
12         Collection c = new ArrayList<>();
13         // 添加元素
14         c.add(new Student("aa", 11, ‘男‘));
15         c.add(new Student("bb", 22, ‘男‘));
16         c.add(new Student("cc", 33, ‘女‘));
17         c.add(new Student("dd", 44, ‘男‘));
18         c.add(new Student("ee", 55, ‘女‘));
19         // 使用迭代器有两种方法,方法一:while循环遍历,优点逻辑结构清晰
20         // 实例化迭代器Iterator对象,注意Iterator是接口,这里是其实现类的对象.
21         Iterator it = c.iterator();
22         while (it.hasNext()) {
23             // it.next()方法返回的是Object对象,而它其实是Student对象,并且重写了toString方法。
24             System.out.println(it.next());
25         }
26
27         // 方法二:for循环遍历迭代器,优点节省内存空间,缺点逻辑结构不那么清晰
28         /*
29             for (Iterator it = c.iterator(); it.hasNext();不需要写表达式) {
30                 System.out.println(it.next());
31             }
32         */
33     }
34 }

原文地址:https://www.cnblogs.com/schiller-hu/p/8450155.html

时间: 2024-08-02 17:24:37

迭代器--遍历集合的专用工具的相关文章

集合,迭代器遍历集合,嵌套集合

集合的框架图 什么是集合? 集合有两个父接口:collection 和  Map collection有两个子接口:List 和 Set List :子接口有两个常用的实现类ArrayList和LinkedList    存储的数据的方式是有序不唯一的 ArrayList其实可以理解为一个可变长度的数组,可以通过索引访问相对应的元素 ArrayList遍历元素和查找指定元素效率比较高 Linkedlist对元素的增删改时效率比较高  而且LinkedList有几个独有的方法,addFrist()

使用迭代器遍历集合

package com.yikuan.cn; import java.util.ArrayList;import java.util.Iterator; public class Test { public static void main(String[] args) { ArrayList<String> a = new ArrayList<>(); a.add("aaa"); a.add("bbb"); a.add("ccc&

增强for循环遍历集合或数组

遍历:for循环遍历数组或集合:iterator迭代器遍历集合:还有增强for循环(for each)遍历数组或集合: 遍历数组: 遍历集合: 原文地址:https://www.cnblogs.com/wmqiang/p/10658532.html

遍历集合的方法

使用增强for循环和迭代器遍历集合 package com.aff.coll; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.Iterator; import org.junit.Test; public class TestFor { // 面试题 @Test public void test3() { String[] str = new Str

Java基础知识强化之集合框架笔记07:Collection集合的遍历之迭代器遍历

1. Collection的迭代器: 1 Iterator iterator():迭代器,集合的专用遍历方式 2. 代码示例: package cn.itcast_03; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; /* * Iterator iterator():迭代器,集合的专用遍历方式 * Iterator(迭代器): * Object next():获取元素,并移动

集合框架(集合的遍历之迭代器遍历)

Iterator iterator() 迭代器,集合的专用遍历方式 package cn.itcast_03; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; /*  *  * .    * .    * .(  */ public class IteratorDemo { public static void main(String[] args) { // 创建集合对象 C

【关于迭代器的for-each遍历集合现象。。。。。】

foreahc迭代集合元素的同时修改集合元素抛异常..ConcurrentModificationException异常 只要使用迭代器遍历,其他集合遍历时进行增删操作都需要留意是否会触发ConcurrentModificationException异常. 一.单线程 1. 异常情况举例 只要抛出出现异常,可以肯定的是代码一定有错误的地方.先来看看都有哪些情况会出现ConcurrentModificationException异常,下面以ArrayList remove 操作进行举例: 使用的数

迭代器遍历map集合的步骤 黑马程序员

迭代器遍历map集合的步骤: Map map = new HashMap(); map.put(1, "a"); map.put(2, "b"); map.put(3, "c"); map.put(4, "d"); Iterator it = map.entrySet().iterator();//取得map集合的中每个键值对所对应的iterator对象 while(it.hasNext()){ String str = it

迭代器遍历【List、Set、Map】&amp;&amp; 遍历集合的方法总结 &amp;&amp; Collections工具类

整理&&总结 迭代器遍历Iterator[List.Set.Map]example 1 package boom.collection; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.HashSet; 6 import java.util.Iterator; 7 import java.util.List; 8 import java.util.Map; 9 import jav