標籤:方法 min 存在 false tor hashcode size string 結束
定義list並將數組放入其中:
List<Integer> temp = new ArrayList<Integer>();
List<String> temp = new ArrayList<String>();
獲得集合內元素個數:list.size();
添加元素:
預設添加:list.add(e);
指定下標添加(添加後下標後的元素向後挪一位):list.add(index,e);
如果我們得到的List還想要增刪,那麼可以用addAll方法
String[] str = {"1", "2", "3", };
List<String> strArray = new ArrayList<String>();
strArray.addAll(Arrays.asList(str));
strArray.add("a");
System.out.println(strArray);
結果為:1 2 3 a
刪除元素:
返回是否刪除:list.remove(e);
直接刪除指定下標的元素(只刪除找到的第一個相符合的元素):list.remove(index);
替換元素(替換掉指定下標的元素):list.set(index,e);
取出元素:list.get(index);
清空集合:list.clear();
判斷集合中是否存在某個元素(存在返回true,不存在返回false):list.contains(e);
對比兩個集合中的所有元素:
兩個對象一定相等:list.equals(list2);
兩個對象不一定相等:list.hashCode() == list2.hashCode();
(兩個相等對象的equals方法一定為true, 但兩個hashcode相等的對象不一定是相等的對象。)
獲得元素下標:
元素存在則返回找到的第一個元素的下標,不存在則返回-1:list.indexOf(e);
元素存在則返回找到的最後一個元素的下標,不存在則返回-1:list.lastIndexOf(e);
判斷集合是否為空白(空則返回true,非空則返回false):list.isEmpty();
返回Iterator集合對象:list.iterator();
將集合轉換為字串:list.toString();
截取集合(從fromIndex開始在toIndex前結束,[fromIndex,toIndex)):list.subList(fromIndex,toIndex);
將集合轉換為數組:
預設類型:list.toArray();
Java------list用法