1.构造函数
*无参构造函数
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
*一个参数的构造方法,参数为初始的容量
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+initialCapacity);
}
}
*参数为集合的构造方法
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
if ((size = elementData.length) != 0) {
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
} else {
this.elementData = EMPTY_ELEMENTDATA;
}
}
//ArrayList集合增加元素
import.java.util.AttayList;
import.java.util.List;
piblic class ListDemo {
public static void main(String[] args) {
List lis = new ArrayList();
lis.add("张三");
lis.add("张三");
lis.add("李四");
lis.add("王五");
System.out.println(lis);
}
}