Java_集合之一
1.Collection集合
1.1數組和集合的區別【理解】
-
相同點
都是容器,可以存儲多個數據
-
不同點
-
數組的長度是不可變的,集合的長度是可變的
-
數組可以存基本數據類型和引用數據類型
集合只能存引用數據類型,如果要存基本數據類型,需要存對應的包裝類
-
1.3Collection 集合概述和使用【應用】
-
Collection集合概述
-
是單例集合的頂層介面,它表示一組對象,這些對象也稱為Collection的元素
-
JDK 不提供此介面的任何直接實現.它提供更具體的子介面(如Set和List)實現
-
-
創建Collection集合的對象
-
多態的方式
-
具體的實現類ArrayList
-
-
Collection集合常用方法
方法名 說明 boolean add(E e) 添加元素 boolean remove(Object o) 從集合中移除指定的元素 boolean removeIf(Object o) 根據條件進行移除 void clear() 清空集合中的元素 boolean contains(Object o) 判斷集合中是否存在指定的元素 boolean isEmpty() 判斷集合是否為空 int size() 集合的長度,也就是集合中元素的個數
1.4Collection集合的遍歷【應用】
-
迭代器介紹
-
迭代器,集合的專用遍歷方式
-
Iterator<E> iterator(): 返回此集合中元素的迭代器,通過集合對象的iterator()方法得到
-
-
Iterator中的常用方法
boolean hasNext(): 判斷當前位置是否有元素可以被取出 E next(): 獲取當前位置的元素,將迭代器對象移向下一個索引位置
-
Collection集合的遍歷
1 public class IteratorDemo1 { 2 public static void main(String[] args) { 3 //創建集合對象 4 Collection<String> c = new ArrayList<>(); 5 6 //添加元素 7 c.add("hello"); 8 c.add("world"); 9 c.add("java"); 10 c.add("javaee"); 11 12 //Iterator<E> iterator():返回此集合中元素的迭代器,通過集合的iterator()方法得到 13 Iterator<String> it = c.iterator(); 14 15 //用while循環改進元素的判斷和獲取 16 while (it.hasNext()) { 17 String s = it.next(); 18 System.out.println(s); 19 } 20 } 21 }
-
迭代器中刪除的方法
void remove(): 刪除迭代器對象當前指向的元素
1 public class IteratorDemo2 { 2 public static void main(String[] args) { 3 ArrayList<String> list = new ArrayList<>(); 4 list.add("a"); 5 list.add("b"); 6 list.add("b"); 7 list.add("c"); 8 list.add("d"); 9 10 Iterator<String> it = list.iterator(); 11 while(it.hasNext()){ 12 String s = it.next(); 13 if("b".equals(s)){ 14 //指向誰,那麼此時就刪除誰. 15 it.remove(); 16 } 17 } 18 System.out.println(list); 19 } 20 }
1.5增強for循環【應用】
-
介紹
-
它是JDK5之後出現的,其內部原理是一個Iterator迭代器
-
實現Iterable介面的類才可以使用迭代器和增強for
-
簡化數組和Collection集合的遍歷
-
-
格式
for(集合/數組中元素的數據類型 變數名 : 集合/數組名) {
// 已經將當前遍歷到的元素封裝到變數中了,直接使用變數即可
}
-
程式碼
1 public class MyCollectonDemo1 { 2 public static void main(String[] args) { 3 ArrayList<String> list = new ArrayList<>(); 4 list.add("a"); 5 list.add("b"); 6 list.add("c"); 7 list.add("d"); 8 list.add("e"); 9 list.add("f"); 10 11 //1,數據類型一定是集合或者數組中元素的類型 12 //2,str僅僅是一個變數名而已,在循環的過程中,依次表示集合或者數組中的每一個元素 13 //3,list就是要遍歷的集合或者數組 14 for(String str : list){ 15 System.out.println(str); 16 } 17 } 18 }
2.List集合
2.1List集合的概述和特點【記憶】
-
List集合的概述
-
有序集合,這裡的有序指的是存取順序
-
用戶可以精確控制列表中每個元素的插入位置,用戶可以通過整數索引訪問元素,並搜索列表中的元素
-
與Set集合不同,列表通常允許重複的元素
-
-
List集合的特點
-
存取有序
-
可以重複
-
有索引
-
2.2List集合的特有方法【應用】
方法介紹
方法名 | 描述 |
---|---|
void add(int index,E element) | 在此集合中的指定位置插入指定的元素 |
E remove(int index) | 刪除指定索引處的元素,返回被刪除的元素 |
E set(int index,E element) | 修改指定索引處的元素,返回被修改的元素 |
E get(int index) | 返回指定索引處的元素 |
示例程式碼
1 public class MyListDemo { 2 public static void main(String[] args) { 3 List<String> list = new ArrayList<>(); 4 list.add("aaa"); 5 list.add("bbb"); 6 list.add("ccc"); 7 //method1(list); 8 //method2(list); 9 //method3(list); 10 //method4(list); 11 } 12 13 private static void method4(List<String> list) { 14 // E get(int index) 返回指定索引處的元素 15 String s = list.get(0); 16 System.out.println(s); 17 } 18 19 private static void method3(List<String> list) { 20 // E set(int index,E element) 修改指定索引處的元素,返回被修改的元素 21 //被替換的那個元素,在集合中就不存在了. 22 String result = list.set(0, "qqq"); 23 System.out.println(result); 24 System.out.println(list); 25 } 26 27 private static void method2(List<String> list) { 28 // E remove(int index) 刪除指定索引處的元素,返回被刪除的元素 29 //在List集合中有兩個刪除的方法 30 //第一個 刪除指定的元素,返回值表示當前元素是否刪除成功 31 //第二個 刪除指定索引的元素,返回值表示實際刪除的元素 32 String s = list.remove(0); 33 System.out.println(s); 34 System.out.println(list); 35 } 36 37 private static void method1(List<String> list) { 38 // void add(int index,E element) 在此集合中的指定位置插入指定的元素 39 //原來位置上的元素往後挪一個索引. 40 list.add(0,"qqq"); 41 System.out.println(list); 42 } 43 }
3.數據結構
3.1數據結構之棧和隊列【記憶】
棧結構
先進後出
隊列結構
先進先出
3.2數據結構之數組和鏈表【記憶】
數組結構
查詢快、增刪慢
隊列結構
查詢慢、增刪快
4.List集合的實現類
4.1List集合子類的特點【記憶】
ArrayList集合
底層是數組結構實現,查詢快、增刪慢
LinkedList集合
底層是鏈表結構實現,查詢慢、增刪快
4.2LinkedList集合的特有功能【應用】
特有方法
方法名 | 說明 |
---|---|
public void addFirst(E e) | 在該列表開頭插入指定的元素 |
public void addLast(E e) | 將指定的元素追加到此列表的末尾 |
public E getFirst() | 返回此列表中的第一個元素 |
public E getLast() | 返回此列表中的最後一個元素 |
public E removeFirst() | 從此列表中刪除並返回第一個元素 |
public E removeLast() | 從此列表中刪除並返回最後一個元素 |
示例程式碼
1 public class MyLinkedListDemo4 { 2 public static void main(String[] args) { 3 LinkedList<String> list = new LinkedList<>(); 4 list.add("aaa"); 5 list.add("bbb"); 6 list.add("ccc"); 7 // public void addFirst(E e) 在該列表開頭插入指定的元素 8 //method1(list); 9 10 // public void addLast(E e) 將指定的元素追加到此列表的末尾 11 //method2(list); 12 13 // public E getFirst() 返回此列表中的第一個元素 14 // public E getLast() 返回此列表中的最後一個元素 15 //method3(list); 16 17 // public E removeFirst() 從此列表中刪除並返回第一個元素 18 // public E removeLast() 從此列表中刪除並返回最後一個元素 19 //method4(list); 20 21 } 22 23 private static void method4(LinkedList<String> list) { 24 String first = list.removeFirst(); 25 System.out.println(first); 26 27 String last = list.removeLast(); 28 System.out.println(last); 29 30 System.out.println(list); 31 } 32 33 private static void method3(LinkedList<String> list) { 34 String first = list.getFirst(); 35 String last = list.getLast(); 36 System.out.println(first); 37 System.out.println(last); 38 } 39 40 private static void method2(LinkedList<String> list) { 41 list.addLast("www"); 42 System.out.println(list); 43 } 44 45 private static void method1(LinkedList<String> list) { 46 list.addFirst("qqq"); 47 System.out.println(list); 48 } 49 } 50 51