package com.fenxiangbe.collection;
import java.util.ArrayList;
import java.util.Collection;
import com.fenxiangbe.bean.Student;
public class Demo_Collection1 {
/**
* A:集合的遍历
* 其实就是依次获取集合中的每一个元素。
* B:案例演示
* 把集合转成数组,可以实现集合的遍历
* toArray()
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("a");
c.add("b");
c.add("c");
c.add("d");
System.out.println(c);
Object[] o = c.toArray();//返回此集合中所有的元素的数组,也是把集合转换成数组的过程
for (int i = 0; i < o.length; i++) {
System.out.println(o[i]);
}
c.clear();
System.out.println("=============");
c.add(new Student("张三" ,23));
c.add(new Student("李四" ,24));
c.add(new Student("王五" ,25));
c.add(new Student("赵六" ,26));
Object[] arr = c.toArray();//集合转换成数组
for (int i = 0; i < arr.length; i++) {
//System.out.println(arr[i]);//遍历数组
Student s = (Student)arr[i];//向下转型
System.out.println(s.getName() + "..." + s.getAge());//调用get方法,取出其中的值
}
}
}