Java中的LinkedList的方法的应用

Java中的LinkedList的方法的应用LinkedList其实也就是我们在数据结构中的链表,这种数据结构有这样的特性:分配内存空间不是必须是连续的;插入、删除操作很快,只要修改前后指针就OK了,时间复杂度为O(1);访问比较慢,必须得从第一个元素开始遍历,时间复杂度为O(n);在Java中,LinkedList提供了丰富的方法,可以模拟链式队列,链式堆栈等数据结构,为用户带来了极大的方便,下面看看这些方法的用法:addboole

大家好,又见面了,我是你们的朋友全栈君。

LinkedList其实也就是我们在数据结构中的链表,这种数据结构有这样的特性:

  • 分配内存空间不是必须是连续的;
  • 插入、删除操作很快,只要修改前后指针就OK了,时间复杂度为O(1);
  • 访问比较慢,必须得从第一个元素开始遍历,时间复杂度为O(n);

在Java中,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]

get

E get(int index):按照下边获取元素;
E getFirst():获取第一个元素;
E getLast():获取第二个元素;

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.get(3);
        System.out.println("get(index): " + linkedList.get(3));

        linkedList.getFirst();
        System.out.println("getFirst: " + linkedList.getFirst());

        linkedList.getLast();
        System.out.println("getLast: " + linkedList.getLast());

        System.out.println(linkedList);
    }
}

输出:

[first, second, second, third, four, five]
get(index): third
getFirst: first
getLast: five
[first, second, second, third, four, five]

注意到了,链表前后没有产生变化。

push、pop、poll

void push(E e):与addFirst一样,实际上它就是addFirst;
E pop():与removeFirst一样,实际上它就是removeFirst;
E poll():查询并移除第一个元素;

public class LinkedListMethodsDemo {
    public static void main(String[] args) {
        LinkedList<String> linkedList = new LinkedList<>();

        linkedList.push("first");
        linkedList.push("second");
        linkedList.push("second");
        linkedList.push("third");
        linkedList.push("four");
        linkedList.push("five");
        System.out.println("linkedList: " + linkedList);

        System.out.println("pop: " + linkedList.pop());
        System.out.println("after pop: " + linkedList);

        System.out.println("poll: " + linkedList.poll());
        System.out.println("after poll: " + linkedList);
    }
}

输出:

linkedList: [five, four, third, second, second, first]
pop: five
after pop: [four, third, second, second, first]
poll: four
after poll: [third, second, second, first]

通过代码示例可以看出:push,pop的操作已经很接近stack的操作了。

如果链表为空的时候,看看poll与pop是啥区别:

public class LinkedListMethodsDemo {
    public static void main(String[] args) {
        LinkedList<String> linkedList = new LinkedList<>();

        System.out.println("poll: " + linkedList.poll());
        System.out.println("pop: " + linkedList.pop());
    }
}

输出:

poll: null
Exception in thread "main" java.util.NoSuchElementException
    at java.util.LinkedList.removeFirst(LinkedList.java:270)
    at java.util.LinkedList.pop(LinkedList.java:801)
    at org.ks.algorithm.LinkedListMethodsDemo.main(LinkedListMethodsDemo.java:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

可以看出poll返回null,而pop则产生异常。

peek

E peek():获取第一个元素,但是不移除;
E peekFirst():获取第一个元素,但是不移除;
E peekLast():获取最后一个元素,但是不移除;

public class LinkedListMethodsDemo {
    public static void main(String[] args) {
        LinkedList<String> linkedList = new LinkedList<>();

        linkedList.push("first");
        linkedList.push("second");
        linkedList.push("second");
        linkedList.push("third");
        linkedList.push("four");
        linkedList.push("five");
        System.out.println("linkedList: " + linkedList);

        System.out.println("peek: " + linkedList.peek());
        System.out.println("peekFirst: " + linkedList.peekFirst());
        System.out.println("peekLast: " + linkedList.peekLast());

        System.out.println("linkedList: " + linkedList);
    }
}

输出:

linkedList: [five, four, third, second, second, first]
peek: five
peekFirst: five
peekLast: first
linkedList: [five, four, third, second, second, first]

如果没找到对应的元素,统统输出null:

public class LinkedListMethodsDemo {
    public static void main(String[] args) {
        LinkedList<String> linkedList = new LinkedList<>();

        System.out.println("linkedList: " + linkedList);
        System.out.println("peek: " + linkedList.peek());
        System.out.println("peekFirst: " + linkedList.peekFirst());
        System.out.println("peekLast: " + linkedList.peekLast());
    }
}

输出:

linkedList: []
peek: null
peekFirst: null
peekLast: null

offer

boolean offer(E e):在链表尾部插入一个元素;
boolean offerFirst(E e):与addFirst一样,实际上它就是addFirst;
boolean offerLast(E e):与addLast一样,实际上它就是addLast;

public class LinkedListMethodsDemo {
    public static void main(String[] args) {
        LinkedList<String> linkedList = new LinkedList<>();

        linkedList.push("first");
        linkedList.push("second");
        linkedList.push("second");
        linkedList.push("third");
        linkedList.push("four");
        linkedList.push("five");
        System.out.println("linkedList: " + linkedList);

        linkedList.offer("six");
        System.out.println("linkedList: " + linkedList);

        linkedList.offerFirst("zero");
        System.out.println("linkedList: " + linkedList);

        linkedList.offerLast("seven");
        System.out.println("linkedList: " + linkedList);
    }
}

输出:

linkedList: [five, four, third, second, second, first]
linkedList: [five, four, third, second, second, first, six]
linkedList: [zero, five, four, third, second, second, first, six]
linkedList: [zero, five, four, third, second, second, first, six, seven]

其他

LinkedList中常用的方法基本都列出来了,当然还有一些其他的例子,这里就一起演示了:

public class LinkedListMethodsDemo {
    public static void main(String[] args) {
        LinkedList<String> linkedList = new LinkedList<>();

        linkedList.push("first");
        linkedList.push("second");
        linkedList.push("second");
        linkedList.push("third");
        linkedList.push("four");
        linkedList.push("five");
        System.out.println("linkedList: " + linkedList);

        System.out.println("linkedList.contains(\"second\"): " + linkedList.contains("second"));
        System.out.println("linkedList.contains(\"six\"): " + linkedList.contains("six"));
        System.out.println("linkedList.element(): " + linkedList.element());
        System.out.println("linkedList: " + linkedList);
        System.out.println("linkedList.set(3, \"set\"): " + linkedList.set(3, "set"));
        System.out.println("linkedList: " + linkedList);
        System.out.println("linkedList.subList(2,4): " + linkedList.subList(2,4));
        System.out.println("linkedList: " + linkedList);
    }
}

输出:

linkedList: [five, four, third, second, second, first]
linkedList.contains("second"): true
linkedList.contains("six"): false
linkedList.element(): five
linkedList: [five, four, third, second, second, first]
linkedList.set(3, "set"): second
linkedList: [five, four, third, set, second, first]
linkedList.subList(2,4): [third, set]
linkedList: [five, four, third, set, second, first]

从输出可以看出,除了set改变原linkedlist,其他几个方法都不改变原链表。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/149328.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 你不知道的PreparedStatement预编译[通俗易懂]

    你不知道的PreparedStatement预编译[通俗易懂]大家都知道,Mybatis内置参数,形如#{xxx}的,均采用了sql预编译的形式,大致知道mybatis底层使用PreparedStatement,过程是先将带有占位符(即”?”)的sql模板发送至mysql服务器,由服务器对此无参数的sql进行编译后,将编译结果缓存,然后直接执行带有真实参数的sql。如果你的基本结论也是如此,那你就大错特错了。目录1.mysql是否默认开启了预编译功…

  • jlink烧录教程_自制flash烧录器

    jlink烧录教程_自制flash烧录器本文主要向大家介绍了Flash基础入门之J-Link固件烧录以及使用J-Flash向arm硬件板下载固件程序,通过具体的内容向大家展现,希望对大家学习Flash基础入门有所帮助。一、始于安装新版的MDK5.11a后,J-Link不能使用,提示安装新固件云云用新版本的STM32集成开发环境MDK5.11a(之前用的4.13a)链接J-Link下载程序,如果J-Link固件版本过低则点击J-Link设…

  • 使用WinINet和WinHTTP实现Http訪问

    使用WinINet和WinHTTP实现Http訪问

  • Tomcat 7 下载地址

    Tomcat 7 下载地址Tomcat7百度云盘下载地址:https://pan.baidu.com/s/1Pvw3kIcCtKcYjaKrq7k-iQ

  • webpack es6转es5原理_webpack和vue cli区别

    webpack es6转es5原理_webpack和vue cli区别首先下载babel-loadernpminstall–save-devbabel-loader@7babel-corebabel-preset-es2015要在最外部输入指令不然会报错然后在webpack.config.js中写相关代码{test:/\.js$/,exclude:/(node_modules|bower_components)/,use:{

  • OSPF协议总结(1)

    OSPF协议总结(1)

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号