List集合在Java日常开发中是必不可少的,只要懂得运用各种各样的方法就可以大大提高我们开发的效率,适当活用各种方法才会使我们开发事半功倍。

  我总结了三种List集合的遍历方式,下面一一来介绍。

  首先来创建一个实体类,以供下面使用。

public class News{
private int id;
private String title;
private String author; public News(int id, String title, String author) {
super();
this.id = id;
this.title = title;
this.author = author;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public String getAuthor() {
return author;
} public void setAuthor(String author) {
this.author = author;
} }

第一种、最基础的遍历方式:for循环,指定下标长度,使用List集合的size()方法,进行for循环遍历

  


import java.util.ArrayList;


public class Demo01 {


  public static void main(String[] args) {

   ArrayList<News> list = new ArrayList<News>();
    
   list.add(new News(1,"list1","a"));
   list.add(new News(2,"list2","b"));
   list.add(new News(3,"list3","c"));
   list.add(new News(4,"list4","d"));
   for (int i = 0; i < list.size(); i++) {
News s = (News)list.get(i);
System.out.println(s.getId()+" "+s.getTitle()+" "+s.getAuthor());
    }
  }
}

第二种、较为简洁的遍历方式:使用foreach遍历List,但不能对某一个元素进行操作(这种方法在遍历数组和Map集合的时候同样适用)

import java.util.ArrayList;

public class Demo02 {

  public static void main(String[] args) {

    ArrayList<News> list = new ArrayList<News>();

     list.add(new News(1,"list1","a"));
     list.add(new News(2,"list2","b"));
     list.add(new News(3,"list3","c"));
     list.add(new News(4,"list4","d"));

    for (News s : list) {
System.out.println(s.getId()+" "+s.getTitle()+" "+s.getAuthor());
   }
  }
}

第三种、适用迭代器Iterator遍历:直接根据List集合的自动遍历

 

import java.util.ArrayList;

public class Demo03 {

  public static void main(String[] args) {

   ArrayList<News> list = new ArrayList<News>();
    
   list.add(new News(1,"list1","a"));
   list.add(new News(2,"list2","b"));
   list.add(new News(3,"list3","c"));
   list.add(new News(4,"list4","d"));
   
Iterator<News> iter = list.iterator();
while (iter.hasNext()) {
News s = (News) iter.next();
System.out.println(s.getId()+" "+s.getTitle()+" "+s.getAuthor());
    }
  }
}

上面三种遍历方式差别不是特别大,在没有特定的要求时,均可以使用。不过根据不同的特殊情况,要合理选择方式,来提高开发的效率。

Java中List集合的三种遍历方式(全网最详)的更多相关文章

  1. Java中list对象的三种遍历方式

    1.增强for循环 for(String str : list) {//其内部实质上还是调用了迭代器遍历方式,这种循环方式还有其他限制,不建议使用. System.out.println(str); ...

  2. Java数组、集合的三种遍历方式(包懂)

    1 for循环 for(int i = 0;i<arr.length;i++){ System.out.print(arr[i]+" "); } 2 foreach循环,这种 ...

  3. java集合的三种遍历方式

    import java.util.ArrayList;  import java.util.Collection;import java.util.Iterator;public class Home ...

  4. Java中Map集合的四种访问方式(转)

    最近学习Java发现集合类型真是很多,访问方式也很灵活,在网上找的方法,先放下备用 public static void main(String[] args) { Map<String, St ...

  5. Java中的HashMap的2种遍历方式比较

    首先我们准备数据,准备一个map Map<String, String> map = new HashMap<String, String>(); for (int i = 0 ...

  6. java中进程与线程--三种实现方式

    一:进程与线程 概述:几乎任何的操作系统都支持运行多个任务,通常一个任务就是一个程序,而一个程序就是一个进程.当一个进程运行时,内部可能包括多个顺序执行流,每个顺序执行流就是一个线程. 进程:进程是指 ...

  7. java中list集合的几种去重方式

    public class ListDistinctExample { public static void main(String[] args) { List<Integer> list ...

  8. python中for循环的三种遍历方式

    #!/usr/bin/env python# -*- coding: utf-8 -*-if __name__ == '__main__': list = ['A', 'B', 'C', 'D'] # ...

  9. 大数据学习day13------第三阶段----scala01-----函数式编程。scala以及IDEA的安装,变量的定义,条件表达式,for循环(守卫模式,推导式,可变参数以及三种遍历方式),方法定义,数组以及集合(可变和非可变),数组中常用的方法

    具体见第三阶段scala-day01中的文档(scala编程基础---基础语法)  1. 函数式编程(https://www.cnblogs.com/wchukai/p/5651185.html): ...

随机推荐

  1. Kali Linux下安装Nessus扫描器

    一.官网下载Nessus(http://www.tenable.com/products/nessus/select-your-operating-system),这里需要查找自己对应的版本,如下图一 ...

  2. S2第一本书内测

    <深入.NET平台和C#编程>内部测试题-笔试试卷 一 选择题 1) 以下关于序列化和反序列化的描述错误的是( C). a) 序列化是将对象的状态存储到特定存储介质中的过程 b) 二进制格 ...

  3. vue-过渡动画

    本篇资料参考于官方文档: http://cn.vuejs.org/guide/transitions.html 概述: Vue 在跳转页面时,提供多种不同方式的动画过渡效果. ●in-out:新元素先 ...

  4. 关于input内容改变的触发时间

    1.onchange onchange 事件会在域的内容改变时触发.支持的标签<input type="text">, <textarea>, <se ...

  5. eclipse如何debug调试jdk源码(任何源码)并显示局部变量

    最近要看struts2源码 仿照了一下查看jdk源码的方式 首先你要有strtus2的jar包和源码,在struts官网上下载时,选择full版本,里面会有src也就是源码了. jar导入项目,保证可 ...

  6. 设置如何远程连接mysql数据库

    安装好mysql5.6.37后,默认情况下,只允许本地登录,禁止远程登录. 可以现在本地安装好连接工具,比如sqlyog或者navicat 登陆后,切换至mysql数据库 执行下面2条语句 '; FL ...

  7. 福州大学W班-Beta冲刺评分

    作业链接 https://edu.cnblogs.com/campus/fzu/FZUSoftwareEngineering1715W/homework/1428 作业要求 1.博客具体要求 昨天的困 ...

  8. 20162308 实验二《Java面向对象程序设计》实验报告

    20162308 实验二<Java面向对象程序设计>实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 ...

  9. 城市安全风险管理项目Postmortem结果

    设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 本系统希望实现快速识别危害因素,使工作人员对风险作出准确的评估.即让使用者熟悉潜在的危险因素,知道 ...

  10. iOS Storyboard unwind segues使用小结

    使用storyboard开发的时候,经常会在一个scene上添加一个button,再拖拽这个button到某个想要关联的页面,最后选择push的方式跳转.这样scene_A和scene_B就有了一个& ...