java的linkedlist详解

先看java之中linkedlist的源码

public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{
    transient int size = 0;

    /**
     * Pointer to first node.
     * Invariant: (first == null && last == null) ||
     *            (first.prev == null && first.item != null)
     */
    transient Node<E> first;

    /**
     * Pointer to last node.
     * Invariant: (first == null && last == null) ||
     *            (last.next == null && last.item != null)
     */
    transient Node<E> last;

    /**
     * Constructs an empty list.
     */
    public LinkedList() {
    }

    /**
     * Constructs a list containing the elements of the specified
     * collection, in the order they are returned by the collection's
     * iterator.
     *
     * @param  c the collection whose elements are to be placed into this list
     * @throws NullPointerException if the specified collection is null
     */
    public LinkedList(Collection<? extends E> c) {
        this();
        addAll(c);
    }

    /**
     * Links e as first element.
     */
    private void linkFirst(E e) {
        final Node<E> f = first;
        final Node<E> newNode = new Node<>(null, e, f);
        first = newNode;
        if (f == null)
            last = newNode;
        else
            f.prev = newNode;
        size++;
        modCount++;
    }

    /**
     * Links e as last element.
     */
     private E unlinkFirst(Node<E> f) {
        // assert f == first && f != null;
        final E element = f.item;
        final Node<E> next = f.next;
        f.item = null;
        f.next = null; // help GC
        first = next;
        if (next == null)
            last = null;
        else
            next.prev = null;
        size--;
        modCount++;
        return element;
    }

首先我们需要复习一下对象的知识,对于一个对象而言,他的赋值只是简单的将该对象的引用进行了赋值,而不是该对象进行了复制。对于函数调用之中的参数,我们也是把对象的引用进行了复制,所以要注意。

从源码之中我们可以看到,linkedlist就是一个链表,他吧原来的对象包装成node之后放到list里面去,删除的时候就把这个node给清空然后调整list就可以了。
对于linkedlist的增删改查:
增加对于原本的对象不进行改变,只是在node的element成员赋值为了该对象的引用
删除:只是把node的element成员赋值为空,并没有改变对象的引用
修改:是对于对象的引用的修改,所以我们的修改会改变原有的东西,请注意
查:没有影响

接下来看一下linkedlist的一些函数

为了模拟queue

我们有
element()展示最前面的元素
peek()展示最前面的元素
offer()加一个元素在最后面
poll()删除最后的元素
remove()删除最后的元素

为了模拟stack

我们有
push()加在最前面的元素(加倍注意这里)
pop()删除最前面元素(加倍注意这里)
peek()展示最前面的元素

建议平常使用的时候就用

addFirst()
addLast()
removeFirst()
removeLast()
getFirst()
getLast()

最后介绍一下两个链表之间怎么复制

注意这里有个坑,直接复制的话,就是原本的list的引用,导致了原本的list改变,复制的list也会改变。
所以使用clone进行浅拷贝,这样的拷贝是没有复制原本list之中的对象的,也就是还是原本对象的引用,对象没有进行复制,代码如下。
必须要记得进行类型强转

LinkedList<Integer> list1=new LinkedList<Integer>();
        LinkedList<Integer> list2=new LinkedList<Integer>();
list1.addFirst(1);
list2=(LinkedList<Integer>)list1.clone();
list1.removeFirst();
        System.out.println(list1);
        System.out.println(list2);
  1. 使用clone进行list复制
  2. clone浅拷贝
  3. clone后强转
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的List是一个有序的集合,可以存储重复元素。List接口继承自Collection接口,提供了一些常用的方法,例如添加、删除、查询等。List接口有多个实现类,例如ArrayListLinkedList、Vector等等,每个实现类都有不同的特点和适用场景。以下是一些常用的List方法: 1. add(E e):在List的末尾添加元素e。 2. add(int index, E e):在List的指定位置index添加元素e。 3. remove(Object o):从List中删除指定元素o。 4. remove(int index):从List中删除指定位置index的元素。 5. set(int index, E e):将List中指定位置index的元素替换为e。 6. get(int index):获取List中指定位置index的元素。 7. size():获取List中元素的个数。 8. indexOf(Object o):返回List中指定元素o的索引,如果不存在则返回-1。 9. subList(int fromIndex, int toIndex):返回List中从fromIndex到toIndex(不包括toIndex)的子列表。 以下是一些常用的List实现类及其特点: 1. ArrayList:基于数组实现,支持快速随机访问元素,但在插入和删除元素时性能较差,适用于读取和遍历操作较多的场景。 2. LinkedList:基于双向链表实现,支持快速插入和删除元素,但在随机访问元素时性能较差,适用于插入和删除操作较多的场景。 3. Vector:与ArrayList类似,但是线程安全,适用于多线程环境。 总之,ListJava中常用的集合类型之一,提供了丰富的API,可以方便地进行元素的添加、删除、查询等操作。在选择List实现类时,需要根据具体的应用场景选择合适的实现类,以获得更好的性能和效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值