Java中的LinkedList的方法的应用
LinkedList其实也就是我们在数据结构中的链表,这种数据结构有这样的特性
- 分配内存空间不是必须是连续的;
- 插入、删除操作很快,只要修改前后指针就OK了,时间复杂度为O(1);
- 访问比较慢,必须得从第一个元素开始遍历,时间复杂度为O(n);
LinkedList提供了丰富的方法,可以模拟链式队列,链式堆栈等数据结构,为用户带来了极大的方便,下面看看这些方法的用法:
add
boolean add(E e):在链表后添加一个元素,如果成功,返回true,否则返回false;
void addFirst(E e):在链表头部插入一个元素;
addLast(E e):在链表尾部添加一个元素;
void add(int index, E element):在指定位置插入一个元素。
public class LinkedListMethodsDemo {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("first");
linkedList.add("second");
linkedList.add("third");
System.out.println(linkedList);
linkedList.addFirst("addFirst");
System.out.println(linkedList);
linkedList.addLast("addLast");
System.out.println(linkedList);
linkedList.add(2, "addByIndex");
System.out.println(linkedList);
}
}
输出:
[first, second, third]
[addFirst, first, second, third]
[addFirst, first, second, third, addLast]
[addFirst, first, addByIndex, second, third, addLast]
remove
E remove();移除链表中第一个元素;
boolean remove(Object o):移除链表中指定的元素;
E remove(int index):移除链表中指定位置的元素;
E removeFirst():移除链表中第一个元素,与remove类似;
E removeLast():移除链表中最后一个元素;
boolean removeFirstOccurrence(Object o):移除链表中第一次出现所在位置的元素;
boolean removeLastOccurrence(Object o):移除链表中最后一次出现所在位置的元素;
public class LinkedListMethodsDemo {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("first");
linkedList.add("second");
linkedList.add("second");
linkedList.add("third");
linkedList.add("four");
linkedList.add("five");
System.out.println(linkedList);
linkedList.remove();
System.out.println("remove: " + linkedList);
linkedList.remove("second");
System.out.println("remove(Object): " + linkedList);
linkedList.remove("six");
System.out.println("remove(Object) not exist: " + linkedList);
linkedList.remove(2);
System.out.println("remove(index): " + linkedList);
linkedList.removeFirst();
System.out.println("removeFirst: " + linkedList);
linkedList.removeLast();
System.out.println("removeLast:" + linkedList);
System.out.println("----------------------------------");
linkedList.clear();
linkedList.add("first");
linkedList.add("second");
linkedList.add("first");
linkedList.add("third");
linkedList.add("first");
linkedList.add("five");
System.out.println(linkedList);
linkedList.removeFirstOccurrence("first");
System.out.println("removeFirstOccurrence: " + linkedList);
linkedList.removeLastOccurrence("first");
System.out.println("removeLastOccurrence: " + linkedList);
}
}
输出:
[first, second, second, third, four, five]
remove: [second, second, third, four, five]
remove(Object): [second, third, four, five]
remove(Object) not exist: [second, third, four, five]
remove(index): [second, third, five]
removeFirst: [third, five]
removeLast:[third]
----------------------------------
[first, second, first, third, first, five]
removeFirstOccurrence: [second, first, third, first, five]
removeLastOccurrence: [second, first, third, five]
E get(int index):按照下边获取元素;
E getFirst():获取第一个元素;
E getLast():获取第二个元素;
版权声明:本文为CSDN博主「K.Sun」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sinat_36246371/article/details/53709625