java容器container ArrayList類
/**
* container容器 contain包含 Collection單例集合 Map雙例集合
* Collection集合中的元素是逐個存放的 ?Map集合中元素成對存儲 KV對 key鍵value值對
* Collection根接口分List子接口和Set子接口 List接口為有序(可通過index索引查找)、元素可重復 Set接口為無序元素不可重復
* ArrayList為List接口的實現(xiàn)類
*/
public class TestArrayList1 {
? ?public static void main(String[] args) {
? ? ? ?List<String> s1 = new ArrayList<>();
? ? ? ?//List<E>接口 E(element) ?ArrayList<E>泛型類implements實現(xiàn)List<E>泛型接口 寫到等式中時兩個E是同一個
? ? ? ?//所以和ArrayList<String> s1 = new ArrayList<>();一樣 ?new右側(cè)的<>不填
? ? ? ?//使用接口引用實現(xiàn)類的好處是不會受到實現(xiàn)類的修改的影響
? ? ? ?s1.add("a");
? ? ? ?//ArrayList實現(xiàn)在Collection接口中定義的方法.add(Object) 實現(xiàn)為向容器中添加一個元素 添加至末尾 返回boolean添加操作是否成功 在添加元素a后容器.size()長度變?yōu)?
? ? ? ?s1.add(0,"b");
? ? ? ?//ArrayList實現(xiàn)在List接口中定義的方法.add(int index,Object) 向容器中指定位置index插入一個元素 該位置原元素及后面的元素全部后移 添加元素b后容器長度變?yōu)?
// ? ? ? ?s1.add(3,"c");報錯Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 2
? ? ? ?//bounds界限 index不能超出容器的.size()長度范圍 s1當前長度為2 index只可以是0,1,2 已存在的元素下標是0,1 所以如果add(2,Object)即為向末尾添加元素
? ? ? ?System.out.println(s1.get(0));
? ? ? ?//ArrayList實現(xiàn)在List接口中定義的方法.get(int index) 返回index索引位的元素 因為b插入0位使得a變?yōu)?位 所以結(jié)果為b
? ? ? ?for (int i=0;i<s1.size();i++){
? ? ? ? ? ?//ArrayList實現(xiàn)在Collection接口中定義的.size()方法 返回容器列表中的元素總數(shù)int 如果超過Integer.MAX_VALUE則返回最大值MAX_VALUE
? ? ? ? ? ?System.out.println(s1.get(i));
? ? ? ? ? ?//遍歷
? ? ? ?}
? ? ? ?for (String s:s1){
? ? ? ? ? ?//for-each遍歷容器
? ? ? ? ? ?System.out.println(s);
? ? ? ?}
? ? ? ?System.out.println(s1.remove(0));
? ? ? ?//ArrayList實現(xiàn)在List接口中定義的.remove(int index)方法 刪除index索引位的元素 該位后面的元素全部前移 方法會返回被刪除的元素 0位為b所以結(jié)果為b
? ? ? ?System.out.println(s1.remove("a"));
? ? ? ?//ArrayList實現(xiàn)在Collection接口中定義的.remove(Object o)方法 刪除傳入的元素/對象 該方法只會刪除1個元素 后面的元素全部前移 返回boolean刪除操作是否成功
? ? ? ?s1.add("c");
? ? ? ?System.out.println(s1.set(0,"d"));
? ? ? ?//List接口的.set(int index,Object o) 替換index索引位的元素為o 返回被替換掉的元素 結(jié)果為c
? ? ? ?s1.clear();
? ? ? ?//Collection接口的.clear() 清空容器 無返回 清空后.size()為0
? ? ? ?System.out.println(s1.isEmpty());
? ? ? ?//Collection接口的.isEmpty() 判斷容器是否為空
? ? ? ?s1.add("e");
? ? ? ?s1.add("f");
? ? ? ?System.out.println(s1.contains("f"));
? ? ? ?//Collection接口的.contains(Object o) 判斷容器中是否包含o 結(jié)果為true
? ? ? ?s1.add("e");
? ? ? ?System.out.println(s1.indexOf("e"));
? ? ? ?//List接口的.indexOf(Object o) 在容器中查找o 存在則返回第一個index 不存在則返回-1
? ? ? ?System.out.println(s1.lastIndexOf("e"));
? ? ? ?//.lastIndexOf(Object o)返回最后一個index 不存在則返回-1
? ? ? ?Object[] o1 = s1.toArray();
? ? ? ?//Collection的.toArray()將單例集合轉(zhuǎn)換為數(shù)組array 返回類型固定Object[]
? ? ? ?//String[] str1 = (String[])o1;報錯[Ljava.lang.Object; cannot be cast to [Ljava.lang.String; Object[]數(shù)組不能強轉(zhuǎn)為String[]
? ? ? ?//當需要轉(zhuǎn)為同泛型類型的數(shù)組時不能使用無參的.toArray()方法
? ? ? ?for (Object o:o1){
? ? ? ? ? ?System.out.println((String) o);
? ? ? ? ? ?//強轉(zhuǎn)只可以對單獨元素進行 不能對集合強轉(zhuǎn)
? ? ? ?}
? ? ? ?String[] str1 = new String[s1.size()];
? ? ? ?s1.toArray(str1);
? ? ? ?//Collection的.toArray(T[] a) 將容器中的元素放到實參指定的數(shù)組中 泛型方法 根據(jù)傳入實參的類型決定返回值類型
? ? ? ?//傳入的參數(shù)必須初始化 且不能為null會報空指針異常 需要創(chuàng)建好數(shù)組的長度 .toArray()方法會對傳進的str1直接修改 方法執(zhí)行后str1已經(jīng)改變了 方法還會將str1返回
? ? ? ?System.out.println(Arrays.toString(str1));
? ? ? ?//str1已經(jīng)被修改 結(jié)果為[e, f, e]
? ? ? ?String[] str01 = s1.toArray(new String[0]);
? ? ? ?System.out.println(str01.length);
? ? ? ?//通過new數(shù)組賦值給引用變量 用一條語句執(zhí)行
? ? ? ?//當實參數(shù)組的長度小于容器.size()時 會生成一個新的數(shù)組 長度為.size()
? ? ? ?List<String> s2 = new ArrayList<>();
? ? ? ?s2.add("1");
? ? ? ?s2.add("2");
? ? ? ?s2.add("1");
? ? ? ?//s1的內(nèi)容為e,f,e ?s2的內(nèi)容為1,2,1
? ? ? ?System.out.println(s1.addAll(s2));
? ? ? ?//Collection的.addAll(Collection<? extends E> c) 將c添加到this容器中 c的泛型類型必須是this的泛型類型的同類或子類 返回boolean操作是否成功 當s2為空時返回false
? ? ? ?System.out.println(s1);
? ? ? ?//ArrayList實現(xiàn)的.addAll() ?c會添加到this的末尾 結(jié)果為[e, f, e, 1, 2, 1] ?addAll即并集 s1為空也可以.addAll(s2)
? ? ? ?s2.clear();
? ? ? ?s2.add("e");
? ? ? ?s2.add("1");
? ? ? ?System.out.println(s1.retainAll(s2));
? ? ? ?//Collection的.retainAll(Collection<?> c) retain保持保留 對s1中現(xiàn)有的元素進行操作 僅保留出現(xiàn)在s2中元素 即求交集 返回boolean 因為是對this容器進行刪減 形參c不需要限定extends E
? ? ? ?System.out.println(s1);
? ? ? ?//結(jié)果[e, e, 1, 1] 只要是符合的元素會保留所有數(shù)量 刪減不符合的元素將后面的元素前移
? ? ? ?s2.remove("1");
? ? ? ?System.out.println(s1.removeAll(s2));
? ? ? ?//Collection的.removeAll(Collection<?> c) 刪除this中存在于c中的元素 即求差集 返回boolean 符合的元素會刪除所有數(shù)量
? ? ? ?System.out.println(s1);
? ? ? ?//結(jié)果[1, 1]
? ? ? ?ArrayList<String> s3 = new ArrayList<>();
? ? ? ?s3.add("1");
? ?}
}