标签:collection
import java.util.*; public class CollectionTest { public static void main(String[] args ){ //1.创建集合 Collection c = new ArrayList(); //2.向集合中添加元素 c.add(1);//jdk1.5之后的自动装箱, c.add(new Integer(10)); Object o = new Object(); c.add(o);//collection 只能存储单个元素,并且存储的元素是引用数据类型。 Customer customer = new Customer("张三",30); c.add(customer); //3.获取元素的个数 System.out.println(c.size()); //4.判断集合是否为空? System.out.println(c.isEmpty()); //5.将集合转换成oject类型的数组 Object[] orray = c.toArray(); for(int i=0;i<orray.length;i++){ System.out.println(orray[i]);//对于Customer类需要重写toString方法。 } //6.清空 c.clear(); System.out.println(c.size()); } } public class Customer { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Customer(String name, int age) { super(); this.name = name; this.age = age; } public Customer(){ } //重写Customer的toString()方法 public String toString(){ return "name=" + name +" "+"age="+ age; } }
本文出自 “gaogaozi” 博客,请务必保留此出处http://hangtiangazi.blog.51cto.com/8584103/1669542
标签:collection
原文地址:http://hangtiangazi.blog.51cto.com/8584103/1669542