Java中的列表接口是什么?
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]
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短