集合框架
- 集合體系根接口:Collection
- 區(qū)別:
- 數(shù)組可以存儲(chǔ)基本數(shù)據(jù)類型和引用數(shù)據(jù)類型
- 集合只能存儲(chǔ)引用數(shù)據(jù)類型==(集合也可以存儲(chǔ)基本數(shù)據(jù)類型,但是在存儲(chǔ)過程中會(huì)自動(dòng)裝箱變成對(duì)象類型)==
- 數(shù)組長度是固定的
- 集合長度是可變的
- 數(shù)組使用for遍歷
- 集合使用**iterator遍歷
- 集合框架體系圖
| list |
set |
| ArrayList(數(shù)組實(shí)現(xiàn)) |
HashSet(哈希算法) |
| LinkedList (鏈表實(shí)現(xiàn)) |
TreeSet(二叉樹算法) |
| x |
LinkedHashSet(鏈表實(shí)現(xiàn)哈希算法) |
集合框架原始實(shí)現(xiàn)
public static void main(String[] args) {
Student[] arr = new Student[5];
arr[0] = new Student("張三", 23);
arr[1] = new Student("李四", 24);
arr[2] = new Student("王五", 25);
for (Student v :
arr) {
System.out.println(v);
}
}
集合框架Collection類
Collection c = new ArrayList();
c.add(new Student("張三", 23));
c.add(new Student("李四", 24));
c.add(new Student("王五", 25));
System.out.println(c);
// boolean add(E e) 添加元素
// boolean remove(Object o) 刪除指定元素
// void clear() 清空元素
// boolean contains(Object o) 檢查是否包含元素
// boolean isEmpty() 是否為空
// int size() 集合長度
- 集合中所有帶All的方法
集合中addAll的方法
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Demo1_Collection {
public static void main(String[] args) {
Collection c1 = new ArrayList();
c1.add("a1");
c1.add("b1");
c1.add("c1");
c1.add("d1");
Collection c2 = new ArrayList();
c2.add("a2");
c2.add("b2");
c2.add("c2");
c2.add("d2");
// 將一個(gè)集合添加到另一個(gè)集合中
c1.addAll(c2);
System.out.println(c1);
}
}
集合中removeAll方法
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Demo1_Collection {
public static void main(String[] args) {
Collection c1 = new ArrayList();
c1.add("a");
c1.add("b");
c1.add("c");
c1.add("d");
Collection c2 = new ArrayList();
c2.add("a");
c2.add("b");
// 刪除集合中重復(fù)的內(nèi)容
c1.removeAll(c2);
System.out.println(c1);
}
}
集合中containsAll方法
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Demo1_Collection {
public static void main(String[] args) {
Collection c1 = new ArrayList();
c1.add("a");
c1.add("b");
c1.add("c");
c1.add("d");
Collection c2 = new ArrayList();
c2.add("a");
c2.add("b");
c2.add("c");
// 判斷集合中判斷是否包含另一個(gè)集合
boolean b = c1.containsAll(c2);
System.out.println(b);
}
}
集合中retainAll方法
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Demo1_Collection {
public static void main(String[] args) {
Collection c1 = new ArrayList();
c1.add("a");
c1.add("b");
c1.add("c");
c1.add("d");
Collection c2 = new ArrayList();
c2.add("a");
c2.add("b");
c2.add("c");
// 取兩個(gè)集合的交集
c1.retainAll(c2);
System.out.println(c1);
}
}
- 集合迭代器
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Demo1_Collection {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("a");
c.add("b");
c.add("c");
c.add("d");
Iterator iterator = c.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。