1.集合类Collection的概述
2.Collection集合遍历以及集合转数组遍历
3.Collection存储自定义对象并遍历练习
4.Iterator迭代器,集合专用的遍历方式
5.Iterator集合迭代器遍历练习
Collection集合遍历以及集合转数组遍历
import java.util.ArrayList;
import java.util.Collection;
public class collectionDemo {
public static void main(String[] args) {
//创建数组对象
Collection c = new ArrayList();
//添加元素
c.add("hello");//Object ojs = "hello" 向上转型
c.add("world");
c.add("Java");
//Obiect[] toArray
Object[] ojs = c.toArray();
//遍历
for(int i = 0 ; i < ojs.length ; i++) {
//System.out.println(ojs[i]);
//我要即输出字符串也要输出字符串长度
//System.out.println(ojs[i]+"-----------"+ojs[i].length());
//很显然这样不行,因为Object没有length()方法
//我们要想使用字符串方法,就必须将元素还原成字符串
//向下转型
String s = (String)ojs[i];
System.out.println(s+"------"+s.length());
}
}
}