Java常用类(LinkedList)

2

Java常用类(LinkedList)

  今天给小伙伴们介绍LinkedList类,LinkedList是Java专门用于处理链表的类,和前面说的ArrayList都是List的实现类,但是ArrayList的底层存储方式是数组,因此查询快,可以通过地址移动直接进行查找,但是要增删元素就很慢,需要大量的移动元素。而LinkedList的底层存储方式是链表,因此查询很慢,需要一个一个比较查找,但是增删元素非常快,只需要修改链表的指向即可。因此模拟队列时常常使用,也是小伙伴们必须掌握的数据结构。

LinkedList类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package demo01;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.function.Predicate;

public class LinkedListClass {
public static void print(LinkedList<Integer> l) {
for (int i: l) {
System.out.print(i + " ");
}
System.out.println();
}
public static void main(String[] args) {
LinkedList<Integer> l = new LinkedList<>();
//public boolean addAll(Collection<? extends E> c) 将集合c中的元素加入到原集合中
//public boolean addAll(int index, Collection<? extends E> c) 在指定位置处将集合c中的元素加入到原集合中
//public boolean add(E e) 向链表尾部添加元素
//public void add(int index, E element) 在指定位置处添加元素
l.add(20);
l.add(0, 10);
l.add(30);

System.out.print("初始时链表l:");
print(l);

//public int size() 查看链表的大小
System.out.println("链表l的大小为:" + l.size());

//public boolean contains(Object o) 查看链表中是否存在元素
System.out.println("链表l是否包含元素20:" + l.contains(20));
System.out.println("链表l是否包含元素40:" + l.contains(40));

//public E get(int index) 获取链表对应索引元素
System.out.println("链表l中索引为1的元素是:" + l.get(1));

//public int indexOf(Object o) 从初始位置开始查找元素对应的索引
System.out.println("链表l中第一个元素10的索引为:" + l.indexOf(10));
//public int lastIndexOf(Object o) 从最后位置开始向前查找元素对应的索引
System.out.println("链表l中最后一个元素20的索引为:" + l.lastIndexOf(20));

//public void addFirst(E e) 向链表头部添加元素
l.addFirst(0);
System.out.print("在链表l头部添加元素0:");
System.out.println(l);

//public void addLast(E e) 向链表尾部添加元素
l.addLast(40);
System.out.print("在链表l尾部添加元素40:");
System.out.println(l);

//public E getFirst() 获取链表头部元素
System.out.println("查看链表l的头部元素:" + l.getFirst());

//public E getLast() 获取链表尾部元素
System.out.println("查看链表l的尾部元素:" + l.getLast());

//public E removeFirst() 移除链表头部元素
l.removeFirst();
System.out.print("移除链表l头部元素:");
System.out.println(l);

//public E removeLast() 移除链表尾部元素
l.removeLast();
System.out.print("移除链表l尾部元素:");
System.out.println(l);

//public E set(int index, E element) 修改链表指定位置元素
l.set(1, 15);
System.out.print("将索引1处改为15时,链表l:");
System.out.println(l);

//public Object[] toArray() 将链表元素转换为数组
Object[] array = l.toArray();
System.out.print("将链表转换为普通数组array:");
for (Object i: array) {
System.out.print(i + " ");
}
System.out.println();

//default boolean removeIf(Predicate<? super E> filter) 移除所有符合条件的元素,需要创建谓词对象
l.removeIf(new Predicate<Integer>() {
@Override
public boolean test(Integer integer) {
return integer == 10;
}
});
System.out.print("移除10时,链表l:");
System.out.println(l);

//public E remove(int index) 移除链表指定索引元素
l.remove(1);
System.out.print("移除索引1的元素,链表l:");
System.out.println(l);

//public void clear() 清空链表
l.clear();
System.out.print("移除所有元素后,链表l:");
System.out.println(l);

//public boolean isEmpty() 判断链表是否为空
System.out.println("链表l是否为空:" + l.isEmpty());
System.out.println("链表l的大小为:" + l.size());

//public static <T> boolean addAll(Collection<? super T> c, T... elements) Collections静态方法,向集合中添加元素
Collections.addAll(l, 20, 10, 40, 30);
System.out.print("添加元素后链表l:");
System.out.println(l);

//public synchronized void sort(Comparator<? super E> c) 对链表进行排序,排序规则要创建比较器对象
l.sort(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1 - o2;
}
});
System.out.print("从小到大排序后链表l:");
System.out.println(l);

//public static void shuffle(List<?> list) 打乱链表中的元素
Collections.shuffle(l);
System.out.print("打乱元素后链表l:");
System.out.println(l);
}
}

1

Java小结

  当然了LinkedList的相关操作还有很多很多,在这里也不可能一一讲解,但是常用的一些操作都已经介绍,尤其是addFirst,addLast,getFirst,getLast,removeFirst,removeLast这些操作,是区别于ArrayList类的关键,希望小伙伴们可以牢记。

-------------本文结束感谢您的阅读-------------
0%