|
- import java.util.*;
- public class CollectionTest {
- /*
- * boolean/add(E e) 确保此 collection 包含指定的元素(可选操作)。
- * void/clear() 移除此 collection 中的所有元素(可选操作)。
- * boolean/contains(Object o) 如果此 collection 包含指定的元素,则返回 true。
- * boolean/cisEmpty() 如果此 collection 不包含元素,则返回 true。
- * iterator() 返回在此 collection 的元素上进行迭代的迭代器。
- * remove(Object o) 从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。
- * size() 返回此 collection 中的元素数。
- * Object[] toArray() 返回包含此 collection 中所有元素的数组。
- * **/
- public static void main(String[] args) {
-
- Collection C =new ArrayList();//新建一个ArratList集合
-
- C.add(1);//集合中只能放引用数据,JDK1.5后的自动装箱
- C.add(new Integer(100));
- Object o = new Object();
- C.add(o);//存储引用类型,是一个内存地址
- Customer cus = new Customer("张三", 24);
- C.add(cus);
-
- System.out.println("集合的元素数量是:"+C.size()+",集合是否为空:"+C.isEmpty());
-
- Object O[] =C.toArray();//把集合转换成一个数组
- for(int i=0;i < O.length;i++){
- System.out.println("集合的第"+i+"元素是:"+O[i]);
- }
-
- C.clear();
- System.out.println("集合的元素数量是:"+C.size()+",集合是否为空:"+C.isEmpty());
- }
- }
-
- class Customer{
- String name;
- int age;
- Customer(String name,int age) {
- this.name=name;
- this.age=age;
- }
- public String toString(){
- return "姓名【"+this.name+"】,年龄【"+this.age+"】";
- }
- }
复制代码 |
|