【Collection集合List集合】
生活随笔
收集整理的這篇文章主要介紹了
【Collection集合List集合】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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集合的遍歷
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.4并發修改異常
-
出現的原因
? 迭代器遍歷的過程中,通過集合對象修改了集合中的元素,造成了迭代器獲取元素中判斷預期修改值和實際修改值不一致,則會出現:ConcurrentModificationException
-
解決的方案
? 用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);} }
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}}*/} }
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() 從此列表中刪除并返回最后一個元素
總結
以上是生活随笔為你收集整理的【Collection集合List集合】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 软件著作权申请流程待发放多久就能到已发放
- 下一篇: 如何选择游戏配音与音乐?