http://www.verejava.com/?id=17159894213940
/**
* 注意:
1. ArrayList 是按照添加的顺序,有序先进先出的.
2. ArrayList 可以添加重复元素
3. ArrayList 也可以添加 空值 null
4. ArrayList 内部是以对象数组 实现的
*/
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
List list = new ArrayList();
// boolean add(Object o)
list.add(1);
list.add(1);
list.add(null);
list.add(2.5);
list.add(true);
list.add("王强");
list.add('A');
list.add(new Cat("波斯猫"));
//
// Object get(int index)
Object wangqiang = list.get(3);
System.out.println(wangqiang);
// int size()
System.out.println(list.size());
//
// boolean contains(Object o)
System.out.println(list.contains("王强"));
// boolean isEmpty()
System.out.println(list.isEmpty());
//
// set(int index, Object element)
list.set(4, 'B');
System.out.println(list.get(4));
//
//遍历集合打印所以元素
for (int i = 0; i < list.size(); i++) {
Object obj = list.get(i);
System.out.print(obj + ",");
}
// boolean remove(int index)
list.remove(4);
System.out.println("");
for (int i = 0; i < list.size(); i++) {
Object obj = list.get(i);
System.out.print(obj + ",");
}
// void clear()
list.clear();
System.out.println("");
System.out.println(list.size());
System.out.println(list.isEmpty());
for (int i = 0; i < list.size(); i++) {
Object obj = list.get(i);
System.out.print(obj + ",");
}
}
}
public class Cat {
private String name;
public Cat(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return name;
}
}