Collection 3

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


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


/*
 *  Collection 层次结构 中的根接口
 * 
 * (集合的遍历)
 *  把集合转换为数组
 * Object[] toArray()返回包含此 collection 中所有元素的数组
 * <T> T[] toArray(T[] a)返回包含此 collection 中所有元素的数组,返回数组的运行时类型与指定数组的运行时类型相同。


 */
public class CollectionDemo3 {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建集合1
Collection c1=new ArrayList();
c1.add("a1");
c1.add("ab2");
c1.add("abc3");
c1.add("abcd4");
//Object[] toArray()返回包含此 collection 中所有元素的数组
Object[] c=c1.toArray();
for(int x=0;x<c.length;x++){
//System.out.println(c[x]);
String s=(String)c[x];
System.out.println(s+"-------"+s.length());
}
}


}
0 0
原创粉丝点击