如何获取数组列表的最后一个值?


当前回答

如果你使用LinkedList代替,你可以通过getFirst()和getLast()访问第一个元素和最后一个元素(如果你想要一个比size() -1和get(0)更干净的方式)

实现

声明一个LinkedList

LinkedList<Object> mLinkedList = new LinkedList<>();

然后这是你可以用来得到你想要的东西的方法,在这种情况下,我们谈论的是列表的FIRST和LAST元素

/**
     * Returns the first element in this list.
     *
     * @return the first element in this list
     * @throws NoSuchElementException if this list is empty
     */
    public E getFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return f.item;
    }

    /**
     * Returns the last element in this list.
     *
     * @return the last element in this list
     * @throws NoSuchElementException if this list is empty
     */
    public E getLast() {
        final Node<E> l = last;
        if (l == null)
            throw new NoSuchElementException();
        return l.item;
    }

    /**
     * Removes and returns the first element from this list.
     *
     * @return the first element from this list
     * @throws NoSuchElementException if this list is empty
     */
    public E removeFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return unlinkFirst(f);
    }

    /**
     * Removes and returns the last element from this list.
     *
     * @return the last element from this list
     * @throws NoSuchElementException if this list is empty
     */
    public E removeLast() {
        final Node<E> l = last;
        if (l == null)
            throw new NoSuchElementException();
        return unlinkLast(l);
    }

    /**
     * Inserts the specified element at the beginning of this list.
     *
     * @param e the element to add
     */
    public void addFirst(E e) {
        linkFirst(e);
    }

    /**
     * Appends the specified element to the end of this list.
     *
     * <p>This method is equivalent to {@link #add}.
     *
     * @param e the element to add
     */
    public void addLast(E e) {
        linkLast(e);
    }

然后你就可以用

mLinkedList.getLast(); 

来获取列表的最后一个元素。

其他回答

如果你使用LinkedList代替,你可以通过getFirst()和getLast()访问第一个元素和最后一个元素(如果你想要一个比size() -1和get(0)更干净的方式)

实现

声明一个LinkedList

LinkedList<Object> mLinkedList = new LinkedList<>();

然后这是你可以用来得到你想要的东西的方法,在这种情况下,我们谈论的是列表的FIRST和LAST元素

/**
     * Returns the first element in this list.
     *
     * @return the first element in this list
     * @throws NoSuchElementException if this list is empty
     */
    public E getFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return f.item;
    }

    /**
     * Returns the last element in this list.
     *
     * @return the last element in this list
     * @throws NoSuchElementException if this list is empty
     */
    public E getLast() {
        final Node<E> l = last;
        if (l == null)
            throw new NoSuchElementException();
        return l.item;
    }

    /**
     * Removes and returns the first element from this list.
     *
     * @return the first element from this list
     * @throws NoSuchElementException if this list is empty
     */
    public E removeFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return unlinkFirst(f);
    }

    /**
     * Removes and returns the last element from this list.
     *
     * @return the last element from this list
     * @throws NoSuchElementException if this list is empty
     */
    public E removeLast() {
        final Node<E> l = last;
        if (l == null)
            throw new NoSuchElementException();
        return unlinkLast(l);
    }

    /**
     * Inserts the specified element at the beginning of this list.
     *
     * @param e the element to add
     */
    public void addFirst(E e) {
        linkFirst(e);
    }

    /**
     * Appends the specified element to the end of this list.
     *
     * <p>This method is equivalent to {@link #add}.
     *
     * @param e the element to add
     */
    public void addLast(E e) {
        linkLast(e);
    }

然后你就可以用

mLinkedList.getLast(); 

来获取列表的最后一个元素。

获取数组列表的最后一个值:

var yourlist = ["1","2","3"];
var lastvalue = yourlist[yourlist.length -1];

它给出的输出是3。

size()方法返回数组列表中元素的个数。元素的下标值从0到(size()-1),因此可以使用myArrayList.get(myArrayList.size()-1)来检索最后一个元素。

使用流API的替代方案:

list.stream().reduce((first, second) -> second)

结果为最后一个元素的Optional。

下面是List接口的一部分(由ArrayList实现):

E e = list.get(list.size() - 1);

E是元素类型。如果列表为空,get抛出IndexOutOfBoundsException异常。你可以在这里找到完整的API文档。