1 為什麼出現集合類
集合長度可變
2 集合體系結構
集合
單列集合
Collection接口
List接口 : 1 有序 2 有索引 3 元素可以重複
ArrayList類 : 數組結構 ,查詢快,增删慢
LinkedList類 : 連結清單結構 ,查詢慢,增删快
Set接口 : 1 無序 2 沒有索引 3 元素唯一
HashSet類
雙列集合
Map接口
HashMap類
3 編寫測試Collection中成員方法使用的案例
public boolean add(E e):添加元素
public boolean remove(Object o):從集合中移除元素
public void clear():清空集合中的元素
public boolean contains(Object o) : 判斷集合中是否存在指定的元素
public boolean isEmpty():判斷集合是否為空
public int size():集合的長度,也就是集合中元素的個數
4 編寫周遊Collection集合的案例
public static void main(String[] args){
Collection<String> c = new ArrayList<>();
c.add("hello");
c.add("world");
c.add("java");
Iterator<String> it = c.iterator();
while(it.hasNext()){
String s = it.next();
System.out.println(s);
}
System.out.println("------------------");
for(String s : c){
System.out.println(s);
}
}
5 編寫Collection集合存儲自定義對象并周遊的案例
public static void main(String[] args) {
// 建立集合對象
Collection<Student> c = new ArrayList<Student>();
// 建立元素對象
Student s1 = new Student("張飛", 30);
Student s2 = new Student("關羽", 32);
Student s3 = new Student("馬超", 28);
Student s4 = new Student("趙雲", 29);
Student s5 = new Student("黃忠", 60);
// 添加元素
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
c.add(s5);
// 周遊集合
// 疊代器
Iterator<Student> it = c.iterator();
while (it.hasNext()) {
Student s = it.next();
// System.out.println(s);
System.out.println(s.getName() + "---" + s.getAge());
}
System.out.println("---------");
for (Student s : c) {
System.out.println(s.getName() + "---" + s.getAge());
}
}
6 闡述List集合的特點并編寫代碼測試
List接口 : 1 有序 2 有索引 3 元素可以重複
7 編寫測試List集合中特有成員方法使用的案例 : ****
public void add(int index,E element):在指定位置添加元素
public E remove(int index):删除指定位置的元素
public E set(int index,E element) : 修改指定位置的元素
public E get(int index):擷取指定位置的元素
8 編寫使用普通for循環周遊List集合的案例
public static void main(String[] args){
List<String> c = new ArrayList<>();
c.add("hello");
c.add("world");
c.add("java");
for(int i = 0 ; i < c.size() ; i++){
String s = c.get(i);
System.out.println(s);
}
}
9 編寫List集合存儲自定義對象并周遊的案例
public static void main(String[] args) {
// 建立集合對象
List<Student> c = new ArrayList<Student>();
// 建立元素對象
Student s1 = new Student("張飛", 30);
Student s2 = new Student("關羽", 32);
Student s3 = new Student("馬超", 28);
Student s4 = new Student("趙雲", 29);
Student s5 = new Student("黃忠", 60);
// 添加元素
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
c.add(s5);
// 周遊集合
// 疊代器
Iterator<Student> it = c.iterator();
while (it.hasNext()) {
Student s = it.next();
// System.out.println(s);
System.out.println(s.getName() + "---" + s.getAge());
}
System.out.println("---------");
for (Student s : c) {
System.out.println(s.getName() + "---" + s.getAge());
}
System.out.println("---------");
for(int i = 0 ; i < c.size() ; i++){
Student s = c.get(i);
System.out.println(s.getName() + "---" + s.getAge());
}
}
10 闡述并發修改異常産生的原因及解決方案
并發修改異常産生的原因 : 疊代器或增強for周遊集合, 采用集合對象修改集合的長度
并發修改異常解決方案 : 1 不用疊代器和增強for(用普通for周遊) 2 疊代器周遊集合,疊代器對象修改集合
11 編寫使用增強for周遊List集合的案例
public static void main(String[] args){
List<String> c = new ArrayList<>();
c.add("hello");
c.add("world");
c.add("java");
for(String s : c){
System.out.println(s);
}
}
12 闡述棧結構和隊列結構的特點
棧結構 : 先進後出
隊列結構 : 先進先出
13 闡述數組結構和連結清單結構的特點
數組結構 : 查詢快,增删慢
連結清單結構 : 查詢慢,增删快
14 闡述List集合子類特點
ArrayList類 : 數組結構 ,查詢快,增删慢
LinkedList類 : 連結清單結構 ,查詢慢,增删快
三種周遊集合的方法:
1.疊代器
2.普通for循環
3.增強for循環
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("12345");
list.add("我是誰");
list.add("admin");
System.out.println(list);
System.out.println("----------------");
list.remove(2);
System.out.println(list);
System.out.println("----------------");
list.get(1);
System.out.println(list.get(1));
System.out.println(list);
System.out.println("----------------");
list.set(1, "大哥!");
System.out.println(list);
System.out.println("----------------");
//普通for循環
for(int i = 0;i <list.size();i++){
String s = list.get(i);
System.out.println(s);
}
System.out.println("----------------");
//疊代器循環
Iterator<String> it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
System.out.println("----------------");
//增強for循環
for (String s : list) {
System.out.println(s);
}
}
輸出結果如下:
[12345, 我是誰, admin]
----------------
[12345, 我是誰]
----------------
我是誰
[12345, 我是誰]
----------------
[12345, 大哥!]
----------------
12345
大哥!
----------------
12345
大哥!
----------------
12345
大哥!