本文共 607 字,大约阅读时间需要 2 分钟。
jdk 5.0 新增了foreach循环,用于遍历集合、数组
for(集合元素的类型 局部变量 : 集合对象)内部仍然调用了迭代器。
//ForeatchTest.javapackage com.ylaihui.collectionitf;import org.junit.Test;import java.util.ArrayList;import java.util.Collection;public class ForeatchTest { @Test public void testremove() { Collection coll = new ArrayList(); coll.add(111); coll.add(222); coll.add("ylaihui"); for(Object obj : coll){ System.out.println(obj); } } @Test public void testArray(){ int[] ints = {1, 2, 3, 4, 5}; for(int i : ints){ System.out.println(i); } }}
转载地址:http://fxuhf.baihongyu.com/