【ArrayList源码】get源码及使用

2019-07-22  本文已影响0人  秀叶寒冬

1 get源码

   /**
     * Returns the element at the specified position in this 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) {
        if (index >= size)//#1
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));//#2

        return (E) elementData[index];//#3
    }

2 get使用

代码:

public class Test {
    
    public static void main(String[] args){
         ArrayList<String> list = new ArrayList<>();
         list.add("wo");
         list.add("ni");
         list.add("ta");
         
         System.out.println(list.get(0));
    }
}

结果:

wo

3 总结

ArrayList的get方法返回ArrayList列表中指定位置的元素。

上一篇 下一篇

猜你喜欢

热点阅读