JAVA中的三种遍历方式
2015-11-21 19:05
507 查看
java中的三种遍历方式
Product product1=new Product("001", "数学班", 10000); Product productX=new Product("001", "英语班", 10000); Product product2=new Product("002", "地理班", 12000); Product product3=new Product("003", "化学班", 9000); ArrayList list =new ArrayList(); //添加元素 list.add(product1); list.add(product2); list.add(product3);
一、普通for循环
list是一个Arraylist对象for (int i = 0; i <list.size(); i++) { Product pro=(Product)list.get(i); System.out.println(pro); }
二、使用迭代器
Iterator itor=list.iterator(); while (itor.hasNext()) { Product pro = (Product)itor.next(); System.out.println(pro); }
//用于for循环的话是这样的
for(<span style="font-family: Arial, Helvetica, sans-serif;">Iterator itor=list.iterator;itor.hasNext();</span>){
<pre name="code" class="java"> Product pro = (Product)itor.next(); System.out.println(pro);
}
三、使用增强型for循环,for each语句
for (Object object:list) { Product pro=(Product)object; System.out.println(pro); }
相关文章推荐
- Java垃圾回收参数配置
- JAVA RMI 原理与实现(转)
- windows下Java环境配置
- 【Java小项目】简单的天气预报
- Web框架对比: Wicket vs Struts
- Java——用户名输入框
- JAVA Socket基础例程
- (转载)ajax请求Struts2返回JSON数据方法
- java.lang.IllegalArgumentException: View not attached to window manager
- No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK
- Java中getResourceAsStream的用法
- 慕课网-java 集合框架
- 关于Spring加载classpath与classpath*的过程剖析
- 慕课网--细说java 多线程 thread与runnable的区别
- Eclipse-JAVA开发环境搭建
- java计算1900到2015共有多少天?
- java时间格式问题
- eclipse配置多个git账号
- maven在Eclipse中的使用
- Eclipse SVN冲突