java遍历list对象集合_java遍历List集合的方法有哪些[通俗易懂]

本文为大家分享了有序集合List中的四种遍历方式,希望能对大家有所帮助。(学习视频分享:java课程)先创建一个Student类,用来创建对象,并提供有参和无参构造方法。packagelesson1;publicclassStudent{Stringname;intage;publicStringgetName(){returnname;}publicvoidsetName…

大家好,又见面了,我是你们的朋友全栈君。

0319598cdf1a8882f108f4fad6e3b01e.png

本文为大家分享了有序集合List中的四种遍历方式,希望能对大家有所帮助。

(学习视频分享:java课程)

先创建一个Student类,用来创建对象,并提供有参和无参构造方法。package lesson1;

public class Student {

String name;

int age;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public Student(String name, int age) {

super();

this.name = name;

this.age = age;

}

public Student() {

super();

// TODO Auto-generated constructor stub

}

}

以下是四种遍历package lesson1;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

public class ListTest {

public static void main(String[] args) {

//使用学生类创建四个对象,并给对象中的属性赋初值

Student s1 = new Student(“zhangsan1”,20);

Student s2 = new Student(“zhangsan2”,21);

Student s3 = new Student(“zhangsan3”,22);

Student s4 = new Student(“zhangsan4”,23);

//创建一个集合

List studentList = new ArrayList();

//将上面的四个学生对象添加到集合中

studentList.add(s1);

studentList.add(s2);

studentList.add(s3);

studentList.add(s4);

// 普通for循环遍历

//for (int i = 0 ; i < studentList.size() ; i++) {

//Student s = (Student)studentList.get(i);

//System.out.println(s.getName());

//System.out.println(s.getAge());

//}

//增强for循环遍历

for (Object os:studentList) {

Student s = (Student)os;

System.out.println(s.getName());

System.out.println(s.getAge());

}

//迭代器遍历

//Iterator it = studentList.iterator();

//while (it.hasNext()) {

//Student s = (Student)it.next();

//System.out.println(s.getName());

//System.out.println(s.getAge());

//}

//jdk 1.8版本提供的forEach()方法遍历,这种方法了解就行

//studentList.forEach((os)->{

//Student s = (Student)os;

//System.out.println(s.getName());

//System.out.println(s.getAge());

//});

}

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/129309.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Pro ASP.NET MVC –第五章 使用Razor「建议收藏」

    Pro ASP.NET MVC –第五章 使用Razor「建议收藏」Pro ASP.NET MVC –第五章 使用Razor

    2022年4月21日
    64
  • java中Collections.sort排序详解[通俗易懂]

    java中Collections.sort排序详解[通俗易懂]Comparator是个接口,可重写compare()及equals()这两个方法,用于比价功能;如果是null的话,就是使用元素的默认顺序,如a,b,c,d,e,f,g,就是a,b,c,d,e,f,g这样,当然数字也是这样的。compare(a,b)方法:根据第一个参数小于、等于或大于第二个参数分别返回负整数、零或正整数。equals(obj)方法:仅当指定的对象也是一个Compara

    2022年7月12日
    20
  • 阿里云服务器使用freessl配置免费证书Nginx

    阿里云服务器使用freessl配置免费证书Nginx环境:阿里云服务器购买的域名服务器:linux+nginxSSL证书:FreeSSL申请的免费证书步骤1、申请ssl证书具体参考二哥的“五分钟搞定HTTPS配置,二哥手把手教”https://blog.csdn.net/qing_gee/article/details/90031376博客,这位大佬写的很详细了2、配置阿里云服务器2.1、上传证书登陆阿里云控制台,搜索“ssl证书应用安全”,上传原有证书,注意一定要将证书转换为pem格式2.2、开启服务器443.

    2025年6月11日
    4
  • python实现简单爬虫功能[通俗易懂]

    python实现简单爬虫功能[通俗易懂]python实现简单爬虫功能,抓取百度贴吧页面中的图片,下载到本地。

    2022年7月3日
    23
  • delphi remobjects

    delphi remobjects概述:RemObjectsSDK是一个先进的远程框架,允许你通过网络远程地访问驻留在服务器上的对象。RemObjectsSDK允许你开发客户/服务应用程序,利用高优化性能的SmartServices或跨平台兼容性的标准WebServices,使客户端和服务器端轻松高效的通讯。高亮特征强大的远程框架为客户端和服务器间有效通讯提供广泛的选项,跨越各种质量的网络(从本地局域网…

    2025年6月16日
    3
  • excel批量导入图片 宏_怎么启用excel的宏

    excel批量导入图片 宏_怎么启用excel的宏准备图片格式规范:C盘的picture文件夹,放100张有序命名的图片,即1.jpg,2.jpg,3.jpg…100.jpg插入图片规则:按照图片的顺序,依次插入表格的A列,让每个图片一个单元

    2022年8月6日
    18

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号