1.Collection集合
1.1集合體系結構【記憶】
-
集合類的特點
提供一種存儲空間可變的存儲模型,存儲的資料容量可以随時發生改變
-
集合類的體系圖
1.2Collection集合概述和基本使用【應用】
- Collection集合概述
- 是單例集合的頂層接口,它表示一組對象,這些對象也稱為Collection的元素
- JDK 不提供此接口的任何直接實作,它提供更具體的子接口(如Set和List)實作
- Collection集合基本使用
public class CollectionDemo01 { public static void main(String[] args) { //建立Collection集合的對象 Collection<String> c = new ArrayList<String>(); //添加元素:boolean add(E e) c.add("hello"); c.add("world"); c.add("java"); //輸出集合對象 System.out.println(c); } }
1.3Collection集合的常用方法【應用】
方法名 | 說明 |
---|---|
boolean add(E e) | 添加元素 |
boolean remove(Object o) | 從集合中移除指定的元素 |
void clear() | 清空集合中的元素 |
boolean contains(Object o) | 判斷集合中是否存在指定的元素 |
boolean isEmpty() | 判斷集合是否為空 |
int size() | 集合的長度,也就是集合中元素的個數 |
1.4Collection集合的周遊【應用】
- 疊代器的介紹
- 疊代器,集合的專用周遊方式
- Iterator iterator():傳回此集合中元素的疊代器,通過集合的iterator()方法得到
- 疊代器是通過集合的iterator()方法得到的,是以我們說它是依賴于集合而存在的
- Collection集合的周遊
public class IteratorDemo {
public static void main(String[] args) {
//建立集合對象
Collection<String> c = new ArrayList<>();
//添加元素
c.add("hello");
c.add("world");
c.add("java");
c.add("javaee");
//Iterator<E> iterator():傳回此集合中元素的疊代器,通過集合的iterator()方法得到
Iterator<String> it = c.iterator();
//用while循環改進元素的判斷和擷取
while (it.hasNext()) {
String s = it.next();
System.out.println(s);
}
}
}
1.5集合使用步驟圖解【了解】
- 使用步驟
1.6集合的案例-Collection集合存儲學生對象并周遊【應用】
-
案例需求
建立一個存儲學生對象的集合,存儲3個學生對象,使用程式實作在控制台周遊該集合
- 代碼實作
- 學生類
public class Student { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
- 測試類
public class CollectionDemo { public static void main(String[] args) { //建立Collection集合對象 Collection<Student> c = new ArrayList<Student>(); //建立學生對象 Student s1 = new Student("林青霞", 30); Student s2 = new Student("張曼玉", 35); Student s3 = new Student("王祖賢", 33); //把學生添加到集合 c.add(s1); c.add(s2); c.add(s3); //周遊集合(疊代器方式) Iterator<Student> it = c.iterator(); while (it.hasNext()) { Student s = it.next(); System.out.println(s.getName() + "," + s.getAge()); } } }
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) | 傳回指定索引處的元素 |
2.3集合的案例-List集合存儲學生對象并周遊【應用】
-
案例需求
建立一個存儲學生對象的集合,存儲3個學生對象,使用程式實作在控制台周遊該集合
- 代碼實作
- 學生類
public class Student { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
- 測試類
public class ListDemo { public static void main(String[] args) { //建立List集合對象 List<Student> list = new ArrayList<Student>(); //建立學生對象 Student s1 = new Student("林青霞", 30); Student s2 = new Student("張曼玉", 35); Student s3 = new Student("王祖賢", 33); //把學生添加到集合 list.add(s1); list.add(s2); list.add(s3); //疊代器方式 Iterator<Student> it = list.iterator(); while (it.hasNext()) { Student s = it.next(); System.out.println(s.getName() + "," + s.getAge()); } System.out.println("--------"); //for循環方式 for(int i=0; i<list.size(); i++) { Student s = list.get(i); System.out.println(s.getName() + "," + s.getAge()); } } }
- 學生類
2.4并發修改異常【應用】
-
出現的原因
疊代器周遊的過程中,通過集合對象修改了集合中的元素,造成了疊代器擷取元素中判斷預期修改值和實際修改值不一緻,則會出現:ConcurrentModificationException
-
源碼
在疊代器的方法中進行掉用了方法checkForComdification();
checkForComdifcation方法:
-
-
解決的方案
用for循環周遊,然後用集合對象做對應的操作即可
- 示例代碼
public class ListDemo { public static void main(String[] args) { //建立集合對象 List<String> list = new ArrayList<String>(); //添加元素 list.add("hello"); list.add("world"); list.add("java"); //周遊集合,得到每一個元素,看有沒有"world"這個元素,如果有,我就添加一個"javaee"元素,請寫代碼實作 // Iterator<String> it = list.iterator(); // while (it.hasNext()) { // String s = it.next(); // if(s.equals("world")) { // list.add("javaee"); // } // } for(int i=0; i<list.size(); i++) { String s = list.get(i); if(s.equals("world")) { list.add("javaee"); } } //輸出集合對象 System.out.println(list); } }
2.5清單疊代器【應用】
- ListIterator介紹
- 通過List集合的listIterator()方法得到,是以說它是List集合特有的疊代器
- 用于允許程式員沿任一方向周遊的清單疊代器,在疊代期間修改清單,并擷取清單中疊代器的目前位置
- 示例代碼
public class ListIteratorDemo { public static void main(String[] args) { //建立集合對象 List<String> list = new ArrayList<String>(); //添加元素 list.add("hello"); list.add("world"); list.add("java"); //擷取清單疊代器 ListIterator<String> lit = list.listIterator(); while (lit.hasNext()) { String s = lit.next(); if(s.equals("world")) { lit.add("javaee"); } } System.out.println(list); } }
可以進行對數組元素修改原因
在ArrayList中的内部類ListIterator中
2.6增強for循環【應用】
- 定義格式
for(元素資料類型 變量名 : 數組/集合對象名) { 循環體; }
- 示例代碼
public class ForDemo { public static void main(String[] args) { int[] arr = {1,2,3,4,5}; for(int i : arr) { System.out.println(i); } System.out.println("--------"); String[] strArray = {"hello","world","java"}; for(String s : strArray) { System.out.println(s); } System.out.println("--------"); List<String> list = new ArrayList<String>(); list.add("hello"); list.add("world"); list.add("java"); for(String s : list) { System.out.println(s); } System.out.println("--------"); //内部原理是一個Iterator疊代器 /* for(String s : list) { if(s.equals("world")) { list.add("javaee"); //ConcurrentModificationException } } */ } }
2.7集合的案例-List集合存儲學生對象三種方式周遊【應用】
-
案例需求
建立一個存儲學生對象的集合,存儲3個學生對象,使用程式實作在控制台周遊該集合
- 代碼實作
- 學生類
public class Student { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
- 測試類
public class ListDemo { public static void main(String[] args) { //建立List集合對象 List<Student> list = new ArrayList<Student>(); //建立學生對象 Student s1 = new Student("林青霞", 30); Student s2 = new Student("張曼玉", 35); Student s3 = new Student("王祖賢", 33); //把學生添加到集合 list.add(s1); list.add(s2); list.add(s3); //疊代器:集合特有的周遊方式 Iterator<Student> it = list.iterator(); while (it.hasNext()) { Student s = it.next(); System.out.println(s.getName()+","+s.getAge()); } System.out.println("--------"); //普通for:帶有索引的周遊方式 for(int i=0; i<list.size(); i++) { Student s = list.get(i); System.out.println(s.getName()+","+s.getAge()); } System.out.println("--------"); //增強for:最友善的周遊方式 for(Student s : list) { System.out.println(s.getName()+","+s.getAge()); } } }
- 學生類
3.資料結構
3.1資料結構之棧和隊列【記憶】
-
棧結構
先進後出
-
隊列結構
先進先出
3.2資料結構之數組和連結清單【記憶】
-
數組結構
查詢快、增删慢
-
連結清單結構
查詢慢、增删快
4.List集合的實作類
4.1List集合子類的特點【記憶】
-
ArrayList集合
底層是數組結構實作,查詢快、增删慢
-
LinkedList集合
底層是連結清單結構實作,查詢慢、增删快
4.2集合的案例-ArrayList集合存儲學生對象三種方式周遊【應用】
-
案例需求
建立一個存儲學生對象的集合,存儲3個學生對象,使用程式實作在控制台周遊該集合
- 代碼實作
- 學生類
public class Student { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
- 測試類
public class ArrayListDemo { public static void main(String[] args) { //建立ArrayList集合對象 ArrayList<Student> array = new ArrayList<Student>(); //建立學生對象 Student s1 = new Student("林青霞", 30); Student s2 = new Student("張曼玉", 35); Student s3 = new Student("王祖賢", 33); //把學生添加到集合 array.add(s1); array.add(s2); array.add(s3); //疊代器:集合特有的周遊方式 Iterator<Student> it = array.iterator(); while (it.hasNext()) { Student s = it.next(); System.out.println(s.getName() + "," + s.getAge()); } System.out.println("--------"); //普通for:帶有索引的周遊方式 for(int i=0; i<array.size(); i++) { Student s = array.get(i); System.out.println(s.getName() + "," + s.getAge()); } System.out.println("--------"); //增強for:最友善的周遊方式 for(Student s : array) { System.out.println(s.getName() + "," + s.getAge()); } } }
- 學生類
4.3LinkedList集合的特有功能【應用】
- 特有方法
方法名 說明 public void addFirst(E e) 在該清單開頭插入指定的元素 public void addLast(E e) 将指定的元素追加到此清單的末尾 public E getFirst() 傳回此清單中的第一個元素 public E getLast() 傳回此清單中的最後一個元素 public E removeFirst() 從此清單中删除并傳回第一個元素 public E removeLast() 從此清單中删除并傳回最後一個元素