天天看點

ArrayList 常用方法 源碼

目錄

ArrayList 常用方法 源碼解析

 0.構造器

1.toArray

2.Set

3.indexof

4.get

5.remove

6.add

7.forEach

8.size

ArrayList 常用方法 源碼解析

 0.構造器

ArrayList 常用方法 源碼
ArrayList 常用方法 源碼

說明:當傳遞的參數為集合類型時,會把集合類型轉化為數組類型,并指派給elementData。

1.toArray

ArrayList 常用方法 源碼

   集合類型轉化為數組類型。

ArrayList 常用方法 源碼

2.Set

public Object set(int i, Object obj)
        {
            rangeCheck(i);                            // 檢驗索引是否合法
            checkForComodification();                 // 舊值
            Object obj1 = elementData(offset + i);    // 賦新值
            elementData[offset + i] = obj;            // 傳回舊值
            return obj1;
        }
           
ArrayList 常用方法 源碼

3.indexof

ArrayList 常用方法 源碼

4.get

ArrayList 常用方法 源碼

5.remove

ArrayList 常用方法 源碼

6.add

ArrayList 常用方法 源碼
ArrayList 常用方法 源碼

插入時

ArrayList 常用方法 源碼
ArrayList 常用方法 源碼

7.forEach

ArrayList 常用方法 源碼

8.size

ArrayList 常用方法 源碼