List接口扩展了Collection 并声明了存储一系列元素的集合的行为。
可以使用从零开始的索引按元素在列表中的位置插入或访问元素。
列表可能包含重复的元素。
除了Collection定义的方法外,List还定义了一些自己的方法,下表中对此进行了概述。
如果无法修改集合,则多个列表方法将引发UnsupportedOperationException,并且当一个对象与另一个对象不兼容时,将生成ClassCastException。
public class CollectionsDemo {
public static void main(String[] args) {
List a1 = new ArrayList();
a1.add("Zara");
a1.add("Mahnaz");
a1.add("Ayan");
System.out.println(" ArrayList Elements");
System.out.print("\t" + a1);
List l1 = new LinkedList();
l1.add("Zara");
l1.add("Mahnaz");
l1.add("Ayan");
System.out.println();
System.out.println(" LinkedList Elements");
System.out.print("\t" + l1);
}
}输出结果
ArrayList Elements [Zara, Mahnaz, Ayan] LinkedList Elements [Zara, Mahnaz, Ayan]