集合的几种遍历
来源:互联网 发布:桌面软件开发语言 编辑:程序博客网 时间:2024/06/03 21:12
步骤:
1、创建学生类
2、创建集合对象
3、创建学生对象
4、把学生对象加入集合对象
5、遍历
package luo2;public class student{ private String name; private int age; public student(){} public student(String name, int age) { super(); this.name = name; this.age = 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; }}
package luo2;import java.util.ArrayList;import java.util.Iterator;public class test { public static void main(String[] args) { ArrayList<student> arry = new ArrayList<student>(); student s1 = new student("林黛玉", 1); student s2 = new student("薛宝钗", 2); student s3 = new student("贾宝玉", 3); student s4 = new student("史湘云" ,4); student s5 = new student("王熙凤", 5); arry.add(s1); arry.add(s2); arry.add(s3); arry.add(s4); arry.add(s5); //迭代器 Iterator<student> it = arry.iterator(); while(it.hasNext()){ student s = it.next(); System.out.println(s.getName()+"*****"+s.getAge()); } System.out.println("----------------"); //普通for循环 for(int x = 0;x<arry.size();x++){ student s = arry.get(x); System.out.println(s.getName()+"*****"+s.getAge()); } System.out.println("----------------"); //增强for循环 for(student s: arry){ System.out.println(s.getName()+"*****"+s.getAge()); } }}
结果:
阅读全文
0 0
- 集合的几种遍历
- 集合遍历的几种方式
- 常见集合的几种遍历方式
- 几种遍历list集合的方法
- 遍历集合的几种方式
- Java几种集合的遍历方法
- Map集合的几种遍历方式
- map集合的几种遍历方式
- 集合的几种遍历方式
- 集合遍历的几种方式
- 几种遍历集合的方法
- 遍历集合的常见的几种方法
- map集合的遍历的几种方式
- Java种遍历Map集合的几种常用方式
- 在Java中遍历Map集合的几种方法
- 【Java】Map集合的几种遍历方式
- 集合的几种遍历,以(list为例)
- Java集合遍历的几种方式总结及比较
- 动态库DLL与LIB之间的区别
- IO流之架设简单的服务器和客户端
- ssm框架
- Kotlin 初步学习
- [poj2451]Uyuw's Concert 半平面交
- 集合的几种遍历
- 动态规划求解思路
- cf841A Godsend
- String类常见用法的实例
- 记录 python3.6.1 tensorflow 安装 可视化
- C语言--回调函数
- java解析excel
- 【Struts2】(1)Struts2框架的概念、struts2的配置文件、Action类文件、Action动态处理函数
- Maven 构建生命周期