集合Set迭代器遍历和数组遍历

		import java.util.Date;
		import java.util.HashSet;
		import java.util.Iterator;
		import java.util.Set;

		public class IteratorAndArray {

			public static void main(String[] args) {
				Set set = new HashSet();
				
				set.add(new String("abc"));
				set.add(new Date());
				set.add(new Integer(20));
				set.add(null);
				
				//迭代器遍历
				Iterator it = set.iterator();
				while(it.hasNext()){        //还有下一个吗?
					Object obj = it.next(); //下一个
					System.out.print(obj + "\t");
					//输出null	Tue Mar 19 21:12:39 CST 2013 abc 20	
				}
				
				System.out.println();
				//数组遍历
				Object[] objs = set.toArray();
				for (int i = 0; i < objs.length; i++) {
					System.out.print(objs[i] + "\t");
					//输出null	Tue Mar 19 21:12:39 CST 2013 abc 20	
				}

			}

		}

展开阅读全文

没有更多推荐了,返回首页