使用三种循环遍历ArrayList类型数组。
来源:互联网 发布:追梦格林体测数据 编辑:程序博客网 时间:2024/06/05 19:15
package 测试;import java.util.ArrayList;import java.util.Iterator;public class EmployArr { public static void main(String[] args) {ArrayList list=new ArrayList ();Employ employ1=new Employ(1, "小王", 23, "2016-1-45");Employ employ2=new Employ(2, "小李", 24, "2016-2-45");Employ employ3=new Employ(3, "小孟", 28, "2016-3-45");Employ employ4=new Employ(4, "小刘", 29, "2016-4-45");list.add(employ1);//添加数组对象。list.add(employ2);list.add(employ3);list.add(employ4);System.out.println("这个元素的数组个数为:"+list.size() );System.out.println("工号\t姓名\t年龄\t入职时间"); //arraylist可以用这种普通for循环。因为是有序的。 for (int i = 0; i < list.size(); i++) { Employ employs =(Employ)list.get(i);System.out.println(employs.id+"\t"+employs.name+"\t"+employs.age+"\t"+employs.timeString);} System.out.println("---------------------------------------------"); System.out.println("工号\t姓名\t年龄\t入职时间"); //用增强for循环 for (Object obj :list ) {Employ emp=(Employ)obj; //因为在集合框架里都是Object类型的,如果要得到对象数组里的属性,要进行强制转换。System.out.println(emp.id+"\t"+emp.name+"\t"+emp.age+"\t"+emp.timeString);} System.out.println("---------------------------------------------");//用迭代器循环 Iterator iterator=list.iterator(); //先给list数组加一个iterator接口。 while ( iterator.hasNext()) { //hasNext判断数组是否还有下一个 Employ em=(Employ) iterator.next();//next用于获取对象// 并行进强转System.out.println(em.id+"\t"+em.name+"\t"+em.age+"\t"+em.timeString ); } }}
0 0
- 使用三种循环遍历ArrayList类型数组。
- ArrayList三种遍历
- java:ArrayList循环遍历的俩种方法使用
- 三种方法遍历ArrayList
- java:ArrayList循环遍历的俩种方法使用 ArrayList取出第i个值
- java:ArrayList循环遍历的俩种方法使用 ArrayList取出第i个值
- 迭代器循环数组和遍历Map的三种方式
- 使用 ArrayList 动态数组遍历目录
- ArrayList动态数组的使用和遍历
- ArrayList循环遍历
- 遍历ArrayList的三种方法
- ArrayList的三种遍历方法
- ArrayList三种遍历方式比较
- js 使用for循环遍历数组
- json数组使用jquery each循环遍历
- List,ArrayList,Map循环遍历
- C# ArrayList循环遍历ListBox
- java怎么循环遍历ArrayList
- [C/C++笔面试]自己实现Strlen,my_strlen最优解
- Miller-Rabin质数测试
- Python学习笔记之四——类型
- 97. Interleaving String
- dfs && bfs
- 使用三种循环遍历ArrayList类型数组。
- Vim十大必备插件
- chrome的插件Postman安装方法
- iOS 开发 用代码打开手电筒
- mysql中查询字段为null或者不为null
- Android网络编程(八)源码解析OkHttp后篇[复用连接池]
- json返回日期格式化的解决
- notification
- OC-数组