在平时的开发过程中使用List的场景很多,你知道List的遍历有多少种方式?今天一起来梳理下List的几种遍历方式。这里以java.util.ArrayList为例来演示。

这里有一个最简单的测试类,里边有一个main方法

<pre class="prettyprint hljs cpp" deep="5" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">package com.my.template.service;import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/*** @date 2022/8/2 18:12*/
public class TestList {public static void main(String[] args) {List<String> list=new ArrayList<String>();list.add("hello");list.add("ArrayList");list.add("!");simpleTraverse(list);}
}

1.最简单的方式

这种方式是最简单的,也是最容易想到的。

<pre class="prettyprint hljs cpp" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">/*** 最简单的遍历方式* @param list*/public static void simpleTraverse(List<String> list){for(int i=0;i<list.size();i++){System.out.println(list.get(i));}}

这种方法就是把List当作一个数组,从数组的第一个位置开始循环到数组的最后位置,有以下几点需要注意,

  1. i的初始值为0,因为数组的第一个下标为0;
  2. 临界值为list的长度-1,也就是“i<list.size()”或“i<=list.size()-1”,这两种均可;

打印结果为:

<pre class="hljs nginx" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">hello
ArrayList
!

这种方式初学者都会的遍历方式,下面看高级点。

2.foreach的遍历方式

上面的遍历方式,下面来看下高级的遍历方法,

<pre class="prettyprint hljs cpp" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">/*** 进阶版的遍历方式,foreach* @param list*/public static void forEachTraverse(List<String> list){for (String str:list) {System.out.println(str);}}

这种方式是利用foreach的用法,很多人不清楚foreach的底层是什么样子的,在idea中找到class文件,看下反编译过来的代码,

从反编译过滤的代码可以看到foreach底层其实是使用的迭代器的方式,也就是下面要说的遍历方式。

3.迭代器的遍历方式

List可以使用迭代器的方式进行遍历是有原因的,因为在list的实现类中均实现了Iterator接口。看下ArrayList中对Iterator接口的实现,

在ArrayList中有静态内部类Itr,该类实现了Iterator接口。同时ArrayList提供了iterator()方法,

这样就可以使用迭代器了。看下迭代器模式的遍历方式,

<pre class="prettyprint hljs dart" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">/*** 迭代器的遍历方式* @param list*/public static void iteratorTraverse(List<String> list){Iterator<String> iterator=list.iterator();while(iterator.hasNext()){System.out.println(iterator.next());}}

看下是不是也是很简单。下面看最后一种遍历方式,流式遍历。

4.流式的遍历方式

所谓流式的遍历,是java8提供的最新的方式,

<pre class="prettyprint hljs php" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">/*** 流式遍历* @param list*/public static void streamTraverse(List<String> list){list.stream().forEach(str->{System.out.println(str);});}

是不是很简单,对于stream()的API后边会专门分析,这里知道这种遍历方式即可,有兴趣的可以先看看该种方式的实现。

总结,本文主要梳理里在日常的开发过程中对List的遍历方式,没有最好的方式只有适合自己的。

Java——list的四种遍历相关推荐

  1. java中高效遍历list_Java中四种遍历List的方法总结(推荐)

    实例如下: package com.ietree.basic.collection.loop; import java.util.ArrayList; import java.util.Iterato ...

  2. Java的四种遍历方式

    package API;import java.util.Iterator; import java.util.List; import java.util.ArrayList;public clas ...

  3. java遍历list_Java中四种遍历List的方法总结(推荐)

    实例如下: package com.ietree.basic.collection.loop; import java.util.ArrayList; import java.util.Iterato ...

  4. Java中Map的 entrySet() 详解以及用法(四种遍历map的方式)

    Entry 由于Map中存放的元素均为键值对,故每一个键值对必然存在一个映射关系.  Map中采用Entry内部类来表示一个映射项,映射项包含Key和Value (我们总说键值对键值对, 每一个键值对 ...

  5. java的entryset_「entryset」Java中Map的 entrySet() 详解以及用法(四种遍历map的方式) - seo实验室...

    entryset Entry 由于Map中存放的元素均为键值对,故每一个键值对必然存在一个映射关系. Map中采用Entry内部类来表示一个映射项,映射项包含Key和Value (我们总说键值对键值对 ...

  6. Java基础14 集合(重要)四种遍历方式 list 并发异常 set

    一.collection 带all的方法 package day14;import java.util.ArrayList; import java.util.Collection;public cl ...

  7. java从1到9构建完全二叉树_Java完全二叉树的创建与四种遍历方法分析

    Java完全二叉树的创建与四种遍历方法分析 发布时间:2020-10-01 11:58:56 来源:脚本之家 阅读:87 作者:泡0沫 本文实例讲述了Java完全二叉树的创建与四种遍历方法.分享给大家 ...

  8. list的四种遍历方式,遍历list集合

    list的四种遍历方式,遍历list集合 list 遍历元素 http://blog.csdn.net/sunrainamazing/article/details/71577662 set遍历元素 ...

  9. Java 中的四种引用

    垃圾收集器与内存分配策略参考目录: 1.判断Java 对象实例是否死亡 2. Java 中的四种引用 3.垃圾收集算法 4. Java9中的GC 调优 5.内存分配与回收策略 在进行垃圾回收之前,虚拟 ...

最新文章

  1. 深度学习时代,调包侠没有未来,但是这个“包”有
  2. Javascript控制Radio HTML控件
  3. 转载:frameset 使用心得
  4. 利用Maya进行论文中网格动画数据的渲染
  5. 在eclipse中运行工程时 出现出现ConnectionProperties 的解决方法
  6. Azure夜校培训第二场2月22日18:00 正式盛情开幕
  7. java下拉模糊查询_select2 智能补全模糊查询select2的下拉选择框使用
  8. [Python] Numpy Learning
  9. 北京四中院在线裁定一起跨国离婚案
  10. uchome中的$_SC:系统全局配置
  11. 为数据库重新生成log文件
  12. sqlmap安装总结
  13. Windows Mobile 6.5开发环境搭建
  14. Python实现文件搜索
  15. 基于SSH开发报刊订阅管理系统的设计与实现
  16. python的MYSQLdb
  17. 美国零售业初创公司排名前5位的软件开发公司
  18. 玉米社:SEM百度竞价推广转化成本过高要如何处理?
  19. 论文阅读_(GIN)How Powerful are Graph Neural Networks
  20. python 手眼标定OpenCV手眼标定(calibrateHandeye())一

热门文章

  1. python培训广西
  2. html输入框初始输入法,【报Bug】input输入框聊天页面,如果输入法默认是全屏手写,input会被挡住,这个能解决吗?...
  3. 呼叫中心系统与网络电话的区别
  4. php imagemagick gif,用R制作gif动态图以及从gif中提取图片
  5. stein算法(求gcd)
  6. vue引用echarts折线平滑面积图
  7. 【机器学习】基于决策树的隐性眼镜选择
  8. 如何实现最佳的跨平台游戏体验?Unity成亮解密实时渲染技术
  9. wireshark应用和数据包简析
  10. 夯实安全根基 | 统信软件与安恒信息达成战略合作