Collection 4

来源:互联网 发布:丽科cad软件下载 编辑:程序博客网 时间:2024/06/15 19:47
package niu.cheng1;


import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;


/*
*  获取功能
* Iterator<E> iterator()返回在此 collection 的元素上进行迭代的迭代器
* E next()返回迭代的下一个元素。 
* boolean hasNext()如果仍有元素可以迭代,则返回 true。(
*/
public class IteratorDemo {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建集合
Collection c1=new ArrayList();
//boolean add(E e)确保此 collection 包含指定的元素(可选操作)。
c1.add("s1");
c1.add("s2");
c1.add("s3");
//Iterator<E> iterator()返回在此 collection 的元素上进行迭代的迭代器
Iterator it=c1.iterator();
//E next()返回迭代的下一个元素。 
//boolean hasNext()如果仍有元素可以迭代,则返回 true。(

/*
//Object ojt=it.next();
//System.out.println(ojt);
System.out.println(it.next());
System.out.println(it.next());
System.out.println(it.next());
*/

//最终版
while(it.hasNext()){
//System.out.println(it.next());
String s=(String)it.next();
System.out.println(s);
}

}


}
0 0
原创粉丝点击