LinkedList用法总结

2019-02-20  本文已影响8人  FoxLayla

LinkedList用法总结

public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, Serializable

双链表可以从头部或尾部双向遍历。

构造函数

常用方法

增添元素

删除元素

查找元素

修改元素

其他方法

// 示例
LinkedList<String> linkedList = new LinkedList<>(); // linkedList: []
boolean result = linkedList.add("a"); // result: true, linkedList: ["a"]
linkedList.addFirst("z"); // linkedList: ["z", "a"]
linkedList.add(1, "b"); // linkedList: ["z", "b", "a"]

List<String> list = new ArrayList<>();
list.add("c");
list.add("d");
list.add("a"); // list: ["c", "d", "a"]
result = linkedList.addAll(list); // linkedList: ["z", "b", "a", "c", "d", "a"]

String firstElement = linkedList.remove(); // linkedList: ["b", "a", "c", "d", "a"]
String specifiedIndexElement = linkedList.remove(3); // linkedList: ["b", "a", "c", "a"]
result = linkedList.remove("a"); // linkedList: ["b", "c", "a"]

result = linkedList.contains("b"); // result: true
String specifiedElement = linkedList.get(2); // specifiedElement: "a"
String lastElement = linkedList.getLast(); // lastElement: "a"
int index = linkedList.indexOf("b"); // index: 0

String element = linkedList.set(1, "a"); // element: "c", linkedList: ["b", "a", "a"]
int sizeOfLinkedList = linkedList.size(); // sizeOfLinkedList: 3
Object[] elements = linkedList.toArray(); // 返回长度为3的Object数组

遍历方式

上一篇下一篇

猜你喜欢

热点阅读