List获取指定位置元素

本文介绍了在ArrayList中如何通过get方法获取指定索引的元素,例如获取第5个元素可以使用list.get(4)。详细解析了相关源码。
摘要由CSDN通过智能技术生成

ArrayList中怎么获取指定第几个元素,例如获取第5个元素,可以通过list.get(4)得到。

源码如下:

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
     private static final long serialVersionUID = 8683452581122892189L;

     private transient Object[] elementData;

     /**
     * Returns the element at the specified position in this list.  返回list中指定位置的元素
     *
     * @param  index index of the element to return         参数,要返回元素的索引
     * @return the element at the specified position in this list 返回,集合中指定位置的元素
     * @throws IndexOutOfBoundsException {@inheritDoc} 抛出数组下标越界异常
     */
    public E get(int index) {
RangeCheck(index);
return (E) elementData[index];
    }



   /**
     * Checks if the given index is in range.  If not, throws an appropriate检查给定的index是否在数组范围内,如果不在,抛出一个合适的运行时异常
     * runtime exception.  Th
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值