java编程--LinkedList详解

一.LinkedList的源码分析
继承关系介绍

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

LinkedList继承了AbstractSequentialList,实现了get等方法
LinkedList实现了Dequed接口,可以作为双端队列使用
LinkedList实现Cloneable接口重写了接口定义的clone()方法,可以使用clone()复制链表
LinkedList实现 java.io.Serializable接口使LinkedList支持序列化
属性介绍

transient int size = 0;//元素个数初始化
transient int size = 0;//定义头结点
transient int size = 0;//定义尾节点

构造函数介绍
其中的有参构造函数进行数据添加操作

public LinkedList() {
    }
     public LinkedList(Collection<? extends E> c) {
        this();
        addAll(c);
    }

内部类
LinkedList中数据的载体,用来存放数据。节点Node有三个属性:前驱节点,后继节点,节点值

private static class Node<E> {
        E item;
        Node<E> next;
        Node<E> prev;

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }

方法介绍
add(E e)该方法用于向列表的末尾添加一个元素,并返回是否添加成功

 public boolean add(E e) {
        linkLast(e);
        return true;
    }

remove(Object o)
该方法用于删除一个给定的元素o,删除第一个在列表中找到的o元素

public boolean remove(Object o) {
        if (o == null) {
            for (Node<E> x = first; x != null; x = x.next) {
                if (x.item == null) {
                    unlink(x);
                    return true;
                }
            }
        } else {
            for (Node<E> x = first; x != null; x = x.next) {
                if (o.equals(x.item)) {
                    unlink(x);
                    return true;
                }
            }
        }
        return false;
    }

addFist(E e)在列表第一个位置添加一个元素

 public void addFirst(E e) {
        linkFirst(e);
    }

addLast(E e)在列表末尾添加一个元素

public void addLast(E e) {
        linkLast(e);
    }

removeFist()用于删除第一个元素,如果列表为空,则抛出NoSuchElementException异常,返回第一个元素的值

 public E removeFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return unlinkFirst(f);
    }

removeLast()用于删除最后一个元素,如果列表为空,则抛出NoSuchElementException的异常,返回最后一个元素的值

 public E removeLast() {
        final Node<E> l = last;
        if (l == null)
            throw new NoSuchElementException();
        return unlinkLast(l);
    }

getFirst()该方法用于返回第一个元素,列表中没有则抛出NoSuchElementException异常

 public E getFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return f.item;
    }

getLast()该方法用于返回列表的最够一个元素,列表中没有则抛出NoSuchElementException的异常

public E getLast() {
        final Node<E> l = last;
        if (l == null)
            throw new NoSuchElementException();
        return l.item;
    }

二ArrayList和LinkedList的联系
ArrayList和LinkedList都是实现了List接口的容器,用于存储一系列的对象引用,他们都可以对元素进行增删改查。
区别:1.ArrayList是实现了基于动态数组的数据结构,LinkedList是基于链表结构
2.对于于随机访问的get和set方法,ArrayList要由于LinkedList,因为LinkedList要移动指针
3.对于新增和删除操作add和removeLinkedList比较占优势,因为ArrayList要移动数组

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值