• Java——list的四种遍历


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

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

    1. "prettyprint hljs cpp" deep="5" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", 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;
    2. import java.util.ArrayList;
    3. import java.util.Iterator;
    4. import java.util.List;
    5. /**
    6. * @date 2022/8/2 18:12
    7. */
    8. public class TestList {
    9. public static void main(String[] args) {
    10. List list=new ArrayList();
    11. list.add("hello");
    12. list.add("ArrayList");
    13. list.add("!");
    14. simpleTraverse(list);
    15. }
    16. }

    1.最简单的方式

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

    1. class="prettyprint hljs cpp" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", 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;">/**
    2. * 最简单的遍历方式
    3. * @param list
    4. */
    5. public static void simpleTraverse(List list){
    6. for(int i=0;i
    7. System.out.println(list.get(i));
    8. }
    9. }

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

    1. i的初始值为0,因为数组的第一个下标为0;
    2. 临界值为list的长度-1,也就是“i

    打印结果为:

    1. <pre class="hljs nginx" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", 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
    2. ArrayList
    3. !

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

    2.foreach的遍历方式

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

    1. class="prettyprint hljs cpp" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", 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;">/**
    2. * 进阶版的遍历方式,foreach
    3. * @param list
    4. */
    5. public static void forEachTraverse(List<String> list){
    6. for (String str:list) {
    7. System.out.println(str);
    8. }
    9. }

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

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

    3.迭代器的遍历方式

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

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

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

    1. class="prettyprint hljs dart" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", 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;">/**
    2. * 迭代器的遍历方式
    3. * @param list
    4. */
    5. public static void iteratorTraverse(List<String> list){
    6. Iterator<String> iterator=list.iterator();
    7. while(iterator.hasNext()){
    8. System.out.println(iterator.next());
    9. }
    10. }

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

    4.流式的遍历方式

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

    1. class="prettyprint hljs php" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", 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;">/**
    2. * 流式遍历
    3. * @param list
    4. */
    5. public static void streamTraverse(List<String> list){
    6. list.stream().forEach(str->{
    7. System.out.println(str);
    8. });
    9. }

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

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

  • 相关阅读:
    Java(常用类01)
    一起Talk Android吧(第四百一十四回:使用三角函数绘制正弦波的优化)
    #成为 SQL 大师#groupby 中不能有聚合函数
    线程同步之条件变量
    密码学-密码协议之零知识证明
    光致发光谱荧光量子效率测量系统
    第一百三十四回 自定义缓冲组件
    软件测试的内容包含什么?
    矩阵分析与应用-13-矩阵的迹
    Java 代码和使用steam流(List对象使用流操作示例,Java正则匹配,获取当前操作系统)
  • 原文地址:https://blog.csdn.net/weixin_62421895/article/details/126174895