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 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短