一.Collection集合
數組: 存儲同一種資料類型的集合容器.
數組的特點:
1. 隻能存儲相同資料類型的資料
2. 一旦給定了長度,數組長度被固定了,不能改變
3. 數組中的元素與元素之間的記憶體位址是連續的
注意: Object類型的數組可以存儲任意類型的資料
集合:集合是存儲對象資料的集合容器。
集合比數組的優勢:
1. 集合可以存儲任意類型的對象資料,數組隻能存儲同一種資料類型 的資料。
2. 集合的長度是會發生變化的,數組的長度是固定的。
注意:
集合雖然說可以儲存任意類型的對象資料 是因為可以通過自動裝箱儲存基本資料類型
Collection 單例集合的根接口
List 如果是實作了List接口的集合類,具備的特點: 有序,可重複。
ArrayList 底層是維護了一個Object數組實作的 特點: 查詢速度快,增删慢。
LinkedList 底層是使用了連結清單資料結構實作的 特點: 查詢速度慢,增删快。
Vector(了解即可) 底層也是維護了一個Object的數組實作的,實作與ArrayList是一樣的,但是Vector是線程安全的,操作效率低。
Set 如果是實作了Set接口的集合類,具備的特點: 無序,不可重複。
HashSet 底層是使用了哈希表來支援的,特點: 存取速度快.
TreeSet 如果元素具備自然順序 的特性,那麼就按照元素自然順序的特性進行排序存儲。
-
Collection接口中的方法:
1.測試collection接口中的方法
// 首先定義Collection接口的
// Collection是個接口 要遵循多态的方式 去建立實作類的對象
Collection collection = new ArraryList();
// 添加(傳回值是布爾類型的)
add() 添加成功傳回true 否則傳回false
addAll(Collection c) 把一個集合 的元素添加到另外一個集合中去。
// 添加一個字元串
boolean b1 = collection.add("a");
// 添加數字
boolean b2 = collection.add();
// 添加對象
boolean b1 = collection.add(new Persom("小東吖",) );
System.out.println(collection);
結果
[a, ]
2.addAll 兩個集合 第一個集合傳到 第一個
// 定義第一個集合
Collection c1 = new ArrayList();
// 定義第二個集合
Collection c2 = new ArrayList();
c1.add("a");
c1.add("b");
// 把傳入的集合中所有的元素 添加到第一個集合中
c2.add("q");
c2.add("w");
c2.add("e");
//c1.addAll(c2);
// 把一個集合當成一個元素傳入到c1集合中
c1.add(c2);
System.out.println(c1);
為什麼能裝基本資料類型?
比如 add()
Object obj = new Integer();
其中有個自動裝箱的操作
結果
[a, b, [q, w, e]]
// 這裡是把一個集合當成一個元素添加進去的
3.測試一些方法
Collection collection = new ArrayList();
collection.add("a");
collection.add("b");
collection.add("c");
collection.add("d");
// 測試方法 判讀這個集合是不是空的
boolean b1 = collection.isEmpty();
System.out.println(b1);
// 擷取集合長度
System.out.println(collection.size());
// 是否包含某個元素
boolean b2 = collection.contains("h");
System.out.println(b2);
// 删除某個元素
boolean b3 = collection.remove("c");
System.out.println("b3 = " + b3);
// 清空集合
collection.clear();
// 列印集合元素
System.out.println(collection);
結果
false
false
b3 = true
[]
4.周遊字元串數組
// 周遊字元串集合
Collection collection = new ArrayList();
collection.add("a");
collection.add("b");
collection.add("c");
collection.add("d");
// 将集合轉化為數組
Object[] array = collection.toArray();
for (int i = ; i < array.length; i++) {
System.out.println(array[i]);
}
結果
a
b
c
d
//建立一個集合 添加三個學生禁區 周遊集合 隻列印學生姓名
// 建立學生的類
public class Student {
// 私有化成員變量
private String name;
private int age;
// 構造方法
public Student() {
}
public Student(String name,int age) {
this.age = age;
this.name = name;
}
// set / get 方法
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
// 直接列印student對象 就可以調用toString方法
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
// 周遊集合
public static void main(String[] args) {
Collection collection = new ArrayList();
// 添加三個對象
collection.add(new Student("小東吖", ));
collection.add(new Student("小東", ));
collection.add(new Student("哈哈", ));
//
// 強轉數組類型 不能把數組中的元素強轉
Object[] array = collection.toArray();
for (int i = ; i < array.length; i++) {
// 數組周遊中要把數組中每一個對象(元素)都進行向下轉型
// 向下轉型.
Student student = (Student)array[i];
System.out.println(student.getName());
}
}
結果
小東吖
小東
哈哈
5.判斷是否全部包含
containsAll(Collection<?> c)
如果此 collection 包含指定 collection 中的所有元素,則傳回 true。
```
``
Collection collection1 = new ArrayList();
Collection collection2 = new ArrayList();
collection1.add("q");
collection1.add("w");
collection1.add("r");
collection2.add("c");
collection2.add("q");
boolean containsAll = collection1.containsAll(collection2);
System.out.println(containsAll);
結果
6.removeAll(Collection
移除此 collection 中那些也包含在指定 collection 中的所有元素(可選操作)。
// 删除重複的
Collection collection1 = new ArrayList();
Collection collection2 = new ArrayList();
collection1.add("q");
collection1.add("w");
collection1.add("r");
collection2.add("r");
collection2.add("s");
// 判斷全部包含時 是可以有重複元素的 也算包含
collection1.removeAll(collection2);
System.out.println(collection1);
[q, w]
7.retainAll(Collection
僅保留此 collection 中那些也包含在指定 collection 的元素(可選操作)。
Collection collection1 = new ArrayList();
Collection collection2 = new ArrayList();
collection1.add("q");
collection1.add("w");
collection1.add("r");
collection2.add("y");
collection2.add("w");
// c1 轉換成 是把兩個集合的交集取出來
// 如果c1沒變 就傳回true
// c1改變了 傳回 false
boolean retainAll = collection1.retainAll(collection2);
System.out.println(collection1);
System.out.println(retainAll);
二.集合疊代器
疊代器(Iterator)
疊代器是一種設計模式,它是一個對象,它可以周遊并選擇序列中的對象
(1) 使用方法iterator()要求容器傳回一個Iterator。第一次調用Iterator的next()方法時,它傳回序列的第一個元素。
注意:iterator()方法是java.lang.Iterable接口,被Collection繼承。
(2) 使用next()獲得序列中的下一個元素。
(3) 使用hasNext()檢查序列中是否還有元素。
(4) 使用remove()将疊代器新傳回的元素删除。
1.疊代器應用:
Collection collection = new ArrayList();
collection.add("a");
collection.add("b");
// 擷取集合中疊代器
Iterator iterator = collection.iterator();
// 判斷有沒有下一個元素
boolean b1 = iterator.hasNext();
// 擷取下一個元素
Object next = iterator.next();
System.out.println(b1);
System.out.println(next);
}
結果
true
a
2.用疊代器周遊集合
Collection collection = new ArrayList();
// 疊代器周遊時 相當于有一個指針 指向你的集合
// 每next一次 指針向後移動
// 最終周遊完成
collection.add("a");
collection.add("b");
collection.add("c");
collection.add("d");
//fun1(collection);
// 使用疊代器循環周遊數組
// 擷取集合中疊代器
Iterator iterator = collection.iterator();
// 判斷是否有下一個元素
while (iterator.hasNext()) {
// 擷取集合中元素
Object next = iterator.next();
System.out.println(next);
}
結果
a
b
c
d