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