本文主要是介绍Java筑基—iterator迭代器和forEach,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Java—集合迭代器
- 一、迭代器概念
- 1、代码展示
- 二、forEach概念
- 1、语法格式
- 2、案例
一、迭代器概念
Java中的迭代器是一种用于遍历集合类中元素的接口。迭代器提供了一种统一的方式来遍历不同类型的集合类,例如List、Set和Map。使用迭代器可以依次访问集合中的每个元素,并对其进行操作。
在Java中,所有实现了Iterable接口的集合类都可以使用迭代器来遍历元素。迭代器提供了以下方法来实现遍历集合:
hasNext()方法:判断当前是否还有元素可以遍历。
next()方法:返回下一个元素并将迭代器的位置向后移动一个位置。
remove()方法:删除当前遍历的元素。
1、代码展示
package com.company.collection;import java.util.Iterator;
import java.util.ArrayList;
import java.util.Collection;public class CollectionPrint {public static void main(String[] args) {Collection c1 = new ArrayList();c1.add("hello");c1.add(66);c1.add(88);c1.add(77);c1.add("study");//直接调用toString方法,对应的c1就是一个字符串打印出来 String类型的整体System.out.println("c1:" + c1);//c1:[hello, 66, 88, 77, study]//第二种:用迭代器判断打印//2.遍历方式二:使用迭代器来遍历集合中的所有元素 更加灵活// 获取当前集合中的迭代器对象Iterator iterator = c1.iterator();// 判断是否有元素可以访问System.out.println(iterator.hasNext());//true// 取出下一个System.out.println(iterator.next());//hello// 判断是否有元素可以访问System.out.println(iterator.hasNext());//true// 取出下一个System.out.println(iterator.next());//66// 判断是否有元素可以访问System.out.println(iterator.hasNext());//true// 取出下一个//编译OK,运行发生NoSuchElementException 没有元素异常
// System.out.println(iterator.next());//NoSuchElementExceptionSystem.out.println("--------------------------------------");//循环 whilewhile (iterator.hasNext()) {System.out.println(iterator.next()); //88 77 study}System.out.println("无数据"); System.out.println("--------------------------------------");Iterator iterator1 = c1.iterator();while (iterator1.hasNext()) {System.out.println(iterator1.next()); // hello 66 88 77 study}System.out.println("-----------------toString---------------------");StringBuilder stringBuilder = new StringBuilder();Iterator iterator2 = c1.iterator();stringBuilder.append("[");while (iterator2.hasNext()) {Object next = iterator2.next();if (!iterator2.hasNext()) {//最后一个对象元素stringBuilder.append(next).append("]");} else {stringBuilder.append(next).append(",").append(" ");}}System.out.println(stringBuilder); //[hello, 66, 88, 77, study]System.out.println("-----------------迭代器删除元素对象---------------------");Iterator iterator3 = c1.iterator();while (iterator3.hasNext()) {Object next = iterator3.next();if ("study".equals(next)) {//迭代器删除元素iterator3.remove();}}System.out.println(c1); // [hello, 66, 88, 77]}}
执行结果:
c1:[hello, 66, 88, 77, study]
true
hello
true
66
true
--------------------------------------
88
77
study
无数据
--------------------------------------
hello
66
88
77
study
-----------------toString---------------------
[hello, 66, 88, 77, study]
-----------------迭代器删除元素对象---------------------
[hello, 66, 88, 77]Process finished with exit code 0
二、forEach概念
1、语法格式
元素类型:数组、集合中的元素/对象的类型
变量名:随便起名称即可
数组/集合名称:要遍历的数组/集合的名称
循环体:对应循环的时候业务逻辑
for(元素类型 变量名 : 数组/集合名称){循环体;
}
2、案例
package com.company.collection;import java.util.ArrayList;
import java.util.Collection;public class ForEachDemo {public static void main(String[] args) {Collection c1 = new ArrayList();//Stringc1.add(new String("hello"));c1.add("hello");//Integerc1.add(Integer.valueOf(77));c1.add(88);c1.add(new Person("kobe",24));System.out.println("集合c1:"+c1);for(Object obj:c1){System.out.println("c1集合对象是:"+obj);}int[] arr = new int[]{22,33,44,55};for (int i:arr){System.out.println("数组内容:"+i);}}
}
结果
集合c1:[hello, hello, 77, 88, Person{name='kobe', age=24}]
c1集合对象是:hello
c1集合对象是:hello
c1集合对象是:77
c1集合对象是:88
c1集合对象是:Person{name='kobe', age=24}
数组内容:22
数组内容:33
数组内容:44
数组内容:55Process finished with exit code 0
这篇关于Java筑基—iterator迭代器和forEach的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!