yuoveyu 发表于 2013-2-3 11:28:32

ForEach集合循环操作

ForEach
package cn.yu.util.print;import java.util.Collection;import java.util.Iterator;/*** 集合循环操作* @author yu jian* @version1.0* @date 2009**/public class ForEach {/**打印数组*/public static <E> void print(Object[] objs){if(objs!=null){for(Object obj : objs){System.out.println(obj);}}}/**打印集合*/public static <E> void print(Iterator<E> iter){while(iter.hasNext()){System.out.println(iter.next());}}/**打印集合*/public static <E> void print(Collection<E> collection){Iterator<E> iter = collection.iterator();while(iter.hasNext()){System.out.println(iter.next());}}/**循环集合,执行自定义操作**/public static <E> void deal(Iterator<E> iter,ForEachCallBack<E> cb){while(iter.hasNext()){cb.doForEach(iter.next());}}/**循环集合,执行自定义操作**/public static <E> void deal(Collection<E> collection,ForEachCallBack<E> cb){Iterator<E> iter = collection.iterator();while(iter.hasNext()){cb.doForEach(iter.next());}}}
ForEachCallBack
package cn.yu.util.print;/*** 循环集合,需要自定义的操作* @author yu jian* @version1.0* @date 2009**/public interface ForEachCallBack<T> {/**自定义的操作**/public void doForEach(T t) ;}
简单的集合打印操作
页: [1]
查看完整版本: ForEach集合循环操作