惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
博客园_首页
W
WeLiveSecurity
S
Secure Thoughts
S
Security @ Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
N
News and Events Feed by Topic
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
T
Tor Project blog
I
Intezer
B
Blog
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
AI
AI
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
P
Privacy International News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
罗磊的独立博客
Vercel News
Vercel News
A
Arctic Wolf
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
H
Heimdal Security Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed

博客园 - 小猫钓鱼吃鱼

把Claude Code玩明白:VS Code零成本接入DeepSeek大模型 硅基流动-免费代金券 告别从零造轮子!我的 Electron+Vue3 脚手架,让你 10 秒启动桌面应用开发 Electron劝退指南?不,这是我的‘真香’致富经 一个面向产品化的 Electron + Vue 3 桌面应用脚手架 动态代理 自己实现HashMap 自己实现Linkedlist,实现其常用的增、删、查的方法 自己实现Arraylsit,实现其常用的几种增、删、该、查的方法 前后端分离 微服务项目 通用后台管理系统 easypoi一行代码搞定excel导入导出 如何跳出页面的Frame框架 如何从GitHub上下载部分自己需要的文件 Mysql Explain 详解 解决 spring boot Failed to decode downloaded font SpringBoot设置首页(默认页)跳转功能的实现方案 idea中搭建ssm框架,详细步骤 微信公众号分享知识 idea创建maven项目速度慢?别急,这有三种方案 ceph是什么 redis 注册开机自启动服务(注意:要到你安装redis的根目录下执行下面的cmd命令) 怎么让mac和win/pc互传文件共享文件传输文件 Starting MySQL... ERROR! The server quit without updating PID file 问题解决
Java中List集合的三种遍历方式(全网最详)
小猫钓鱼吃鱼 · 2020-03-19 · via 博客园 - 小猫钓鱼吃鱼

介绍3种方式遍历list集合

1 创建一个model

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());
    }
  }
}