Java集合——List接口

1.定义

List是Collection的子接口,元素有序并且可以重复,表示线性表。

2.方法

boolean add(E e):在列表末添加元素

void add(int index, E e):在指定索引处添加元素

boolean addAll(int index, Collection e):在指定索引处放入集合

Object get(int index):获取指定索引的元素

int indexOf(Object o):获取指定元素在集合中的索引(第一次出现的元素)

int lastIndexOf(Object o):返回此列表中最后出现的元素的索引

Object remove(int index):删除指定索引对应的元素

boolean remove(Object o):删除指定元素

void clear():删除列表所有元素

boolean contains(Object o):判断列表是否包含某元素

Object set(int index, Object e):替换指定索引的元素

List subList(int fromIndex, int toIndex):返回索引为[form,to)的元素集合

boolean isEmpty():判断列表是否为空

int size():返回列表长度

Iterator<E> iterator():列表遍历的迭代器

其他方法及其具体说明:https://docs.oracle.com/javase/8/docs/api/

3.常用实现类

ArrayList:它为元素提供了下标,可以看作长度可变的数组,为顺序线性表。

LinkedList:通过代价较低的在List中间进行插入和删除操作,提供了优化的顺序访问,但是在随即访问方面相对比较慢,为链表线性表。

4.实例

 1 import java.util.*;
 2 
 3 public class ListFunc {
 4     public static void main(String[] args){
 5         List<String> list1 = new ArrayList<>();
 6 
 7         //在列表末尾添加元素
 8         list1.add("1");
 9         list1.add("2");
10         list1.add("3");
11         list1.add("4");
12 
13         // 在列表指定索引处添加元素
14         list1.add(1,"2");
15         list1.add(5,"5");
16         list1.add(6,"6");
17         System.out.println(list1);     // [1, 2, 2, 3, 4, 5, 6]
18 
19         //删除指定索引的元素
20         list1.remove(0);
21         System.out.println(list1);  // [2, 2, 3, 4, 5, 6]
22 
23         //删除指定元素(第一个),要删除的元素可以不在列表中
24         list1.remove("2");
25         System.out.println(list1);  //[2, 3, 4, 5, 6]
26 
27         //删除指定索引区域的多个元素
28         // subList = list1.subList(1,3);
29         // subList.clear();                 // 两者等价,更改subList会改变list1
30         list1.subList(1,3).clear();
31         System.out.println(list1);   //[2, 5, 6]
32 
33         //删除所有元素
34 //        list1.clear();
35 //        System.out.println(list1);   //[]
36 
37         //获取指定索引处元素
38         String a1 = list1.get(0);
39         System.out.println(a1);   // "2"
40 
41         //获取指定索引区域的多个元素
42         List<String> list2 = list1.subList(0,2);
43         System.out.println(list2);   // [2, 5]
44 
45         // 修改指定索引的元素
46         list2.set(0,"1");
47         System.out.println(list2);  // [1, 5]
48 
49         // 获取指定元素的索引,没有该值返回-1
50         int index = list2.indexOf("1");
51         System.out.println(index);   // 0
52 
53         // 判断列表是否有某元素
54         boolean flag = list2.contains("5");
55         System.out.println(flag);  // true
56 
57         // list转array
58         String[] arr2 = list2.toArray(new String[0]);
59 //        String[]  arr2 = list2.toArray(new String[list2.size()]);    //两者等价
60         System.out.println(Arrays.toString(arr2));   // [1, 5]
61 
62         // iterator迭代器
63         Iterator<String> it = list2.iterator();
64         while (it.hasNext()){
65             System.out.print(it.next()+" ");      // 1 5
66         }
67 
68         // 增强for循环
69         for(String str: list2){
70             System.out.print(str+ " ");  // 1 5
71         }
72     }
73 }

!!!

猜你喜欢

转载自www.cnblogs.com/jfl-xx/p/4707183.html