- List 不能实例化,因为list是一个接口,可以添加任何类型对象
可以实例化一个ArrayList(动态数组)或LinkedList(链表),还有Vector(动态数组)和Stack(实现堆栈)
List my list = new ArrayList();
package LearnJava;
import java.util.*;
public class learnList{
public static void main(String[] args){
ArrayList list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
System.out.println("before: "+list.size());
list.remove(0);
System.out.println("after: "+list.size());
//迭代器遍历list
Iterator it = list.iterator();
while(it.hasNext()){
int numbs = (Integer) it.next();
System.out.println(numbs);
}
}
}
result:
before: 4
after: 3
2
3
4