LinkedList源码分析(五)

## LinkedList源码分析()
/**
     * 更新元素中指定下标元素
     *
     * @param index 元素下标
     * @param e     修改的元素
     * @return 返回修改前的数据
     */
    public E set(int index, E e) {
        checkIndex(index);
        Node<E> n = node(index);
        E oldValue = n.item;
        n.item = e;

        return oldValue;
    }

    /**
     * 获取第一个节点元素
     *
     * @return 第一个Node保存内容
     */
    public E getFirst() {
        Node<E> f = first;

        if (null == f) {
            throw new NoSuchElementException();
        }

        return f.item;
    }

    /**
     * 获取最后一个节点元素
     *
     * @return 最后一个Node保存内容
     */
    public E getLast() {
        Node<E> l = last;

        if (null == l) {
            throw new NoSuchElementException();
        }

        return l.item;
    }

    /**
     * 获取指定下标(指定计数,第几个节点内存储的元素)
     *
     * @param index 指定的下标位置,计数
     * @return 对应的元素内容
     */
    public E get(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException();
        }

        if (index < (size >> 1)) {
            Node<E> n = first;
            for (int i = 0; i < index; i++) {
                n = n.next;
            }

            return n.item;
        } else {
            Node<E> n = last;
            for (int i = size - 1; i > index; i--) {
                n = n.prev;
            }

            return n.item;
        }
    }

    /**
     * 获取指定元素的第一个下标位置
     *
     * @param obj 指定元素
     * @return 返回元素的下标位置
     */
    public int indexOf(Object obj) {
        int index = 0;
        if (obj == null) {
            for (Node<E> f = first; f != null; f = f.next) {
                if (f.item == null) {
                    return index;
                }
                index++;
            }
        } else {
            for (Node<E> f = first; f != null; f = f.next) {
                return index;
            }
            index++;
        }
        return -1;
    }

    /**
     * 返回指定元素最后一次出现的下标位置
     *
     * @param obj 指定元素
     * @return 指定元素最后一次出现的位置
     */
    public int lastIndexOf(Object obj) {
        int index = size;
        if (obj == null) {
            for (Node<E> l = last; l != null; l = l.prev) {
                index--;
                if (l.item == null)
                    return index;
            }
        } else {
            for (Node<E> l = last; l != null; l = l.prev) {
                index--;
                if (obj.equals(l.item))
                    return index;
            }
        }
        return -1;
    }

    /**
     * 判断元素是否在集合中
     *
     * @param obj 需要判断的元素
     * @return 返回为true表示存在
     */
    public boolean contains(Object obj) {
        return indexOf(obj) != -1;
    }

    /**
     * LinkedList集合转化成Object数组
     *
     * @return 返回Object数组
     */
    public Object[] toArray() {
        Object[] ret = new Object[size];
        int i = 0;
        for (Node<E> f = first; f != null; f = f.next) {
            ret[i++] = f.item;
        }

        return ret;
    }

    /**
     * 返回LinkedList集合从指定下标截取到指定下标的新LinkedList集合
     *
     * @param start 开始下标
     * @param end   结束下标
     * @return 返回新的集合
     */
    public List<E> subList(int start, int end) {
        checkSubIndex(start, end);
        LinkedList<E> newLinkedList = new LinkedList<>();
        for (Node<E> n = node(start); n != node(end); n = n.next) {
            newLinkedList.add(n.item);
        }
        return (List<E>) newLinkedList;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值