日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Java14-day05【集合(Collection常用方法-遍历、List特有方法、List集合子类特点、LinkedList集合的特有功能、ListIterator)】

發布時間:2024/9/30 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java14-day05【集合(Collection常用方法-遍历、List特有方法、List集合子类特点、LinkedList集合的特有功能、ListIterator)】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • 視頻+資料(工程源碼、筆記)【鏈接:https://pan.baidu.com/s/1MdFNUADVSFf-lVw3SJRvtg? ?提取碼:zjxs】
  • Java基礎--學習筆記(零起點打開java世界的大門)--博客匯總表

目? ?錄

01_集合體系結構

1.1、集合知識回顧

1.2、集合類體系結構

02_Collection集合概述和使用

1.3、Collection集合概述和使用

03_Collection集合常用方法

1.4、Collection集合常用方法

04_Collection集合的遍歷

1.5、Collection集合的遍歷

05_集合使用步驟圖解

1.6、集合的使用步驟圖解

06_Collection集合存儲學生對象并遍歷

案例:Collection集合存儲學生對象并遍歷

07_List集合概述和特點

2.1、List集合概述和特點

08_List集合的特有方法

2.2、List集合特有方法

09_List集合存儲學生對象并遍歷

案例:List集合存儲學生對象并遍歷

10_并發修改異常

2.3、并發修改異常

并發修改異常的源碼分析

11_列表迭代器

2.4、ListIterator

ListIterator源碼分析

12_增強for循環

2.5、增強for循環

13_List集合存儲學生對象三種方式遍歷

案例:List集合存儲學生對象用三種方式遍歷

14_數據結構之棧和隊列

2.6、數據結構

2.7、常見數據結構之棧

2.8、常見數據結構之隊列

15_數據結構之數組和鏈表

2.9、常見數據結構之數組

2.10、常見數據結構之鏈表

16_List集合子類的特點

2.11、List集合子類特點

17_ArrayList集合存儲學生對象三種方式遍歷

案例:ArrayList集合存儲學生對象,用三種方式遍歷

18_LinkedList集合的特有功能

2.12、LinkedList集合的特有功能


01_集合體系結構

1.1、集合知識回顧

集合類的特點:提供一種存儲空間可變的存儲模型,存儲的數據容量可以隨時發生改變。

1.2、集合類體系結構

接口不能直接創建對象(實例化),必須通過具體的實現類來創建對象。

02_Collection集合概述和使用

1.3、Collection集合概述和使用

Collection集合概述

  • 是單例集合的頂層接口,它表示一組對象,這些對象也稱為Collection的元素。
  • JDK 不提供此接口的任何直接實現,它提供更具體的子接口(如Set和List)實現。

創建Collection集合的對象

  • 多態的方式
  • 集體的實現類ArrayList

泛型:集合中的元素類型。

03_Collection集合常用方法

1.4、Collection集合常用方法

Alt+7 ? 打開一個窗口,能夠看到類的所有信息。

查看ArrayList的add()方法源碼:

04_Collection集合的遍歷

1.5、Collection集合的遍歷

Iterator:迭代器,集合的專用遍歷方式

  • Iterator<E> iterator():返回此集合中元素的迭代器,通過集合的iterator()方法得到。
  • 迭代器是通過集合的iterator()方法得到的,所以說它是依賴于集合而存在的。

Iterator中的常用方法

  • E next():返回迭代中的下一個元素(獲取集合元素)。
  • boolean hasNext():如果迭代具有更多元素,則返回 true。

迭代器中的泛型,與集合中的泛型一致。

// Iterator<E> iterator():返回此集合中元素的迭代器,通過集合的iterator()方法得到 Iterator<String> it = c.iterator(); // 多態方式得到Iterator對象 返回Iterator接口的具體實現類:Itr /* c.iterator()具體實現方法 --> ArrayListpublic Iterator<E> iterator() { // Iterator接口return new Itr(); // 返回Iterator接口的具體實現類對象}private class Itr implements Iterator<E> { // Itr類 定義在ArrayList中... // ArrayList中的Itr類}*/

?

05_集合使用步驟圖解

1.6、集合的使用步驟圖解

06_Collection集合存儲學生對象并遍歷

案例:Collection集合存儲學生對象并遍歷

07_List集合概述和特點

2.1、List集合概述和特點

List集合概述

  • 有序集合(也稱為序列),用戶可以精確控制列表中每個元素的插入位置。用戶可以通過整數索引訪問元素,并搜索列表中的元素。
  • 與Set集合不同,列表通常允許重復的元素。

List集合特點

  • 有序(有索引):存儲和取出的元素順序一致。
  • 可重復:可以存儲重復元素。
  • 元素存取有序。

List接口繼承自Collection接口,Collection接口中的功能 List接口都可以使用。

08_List集合的特有方法

2.2、List集合特有方法

09_List集合存儲學生對象并遍歷

案例:List集合存儲學生對象并遍歷

10_并發修改異常

2.3、并發修改異常

并發修改異常

  • ConcurrentModificationException

產生原因

  • 迭代器遍歷的過程中,通過集合對象修改了集合中元素的長度,造成了迭代器獲取元素中判斷 預期修改值和實際修改值 不一致,則會出現:ConcurrentModificationException。

解決方案

  • 用for循環遍歷,然后用集合對象做對應的操作即可。

并發修改異常的源碼分析

public interface List<E> {Iterator<E> iterator();boolean add(E e); }public abstract class AbstractList<E> {protected int modCount = 0; }public class ArrayList<E> extends AbstractList<E> implements List<E> {public E get(int index) {Objects.checkIndex(index, size);return elementData(index);}public boolean add(E e) {modCount++;add(e, elementData, size);return true;}public Iterator<E> iterator() {return new Itr();}private class Itr implements Iterator<E> {int expectedModCount = modCount;/*modCount:實際修改集合的次數expectedModCount:預期修改集合的次數*/public E next() {checkForComodification();int i = cursor;if (i >= size)throw new NoSuchElementException();Object[] elementData = ArrayList.this.elementData;if (i >= elementData.length)throw new ConcurrentModificationException();cursor = i + 1;return (E) elementData[lastRet = i];}final void checkForComodification() {if (modCount != expectedModCount)throw new ConcurrentModificationException();}}}

get()方法,不會做“實際修改值 ==?預期修改值”的判斷。∴,不會出現并發修改異常。

11_列表迭代器

2.4、ListIterator

ListIterator源碼分析

public interface List<E> {Iterator<E> iterator();ListIterator<E> listIterator(); }public abstract class AbstractList<E> {protected int modCount = 0; }public class ArrayList<E> extends AbstractList<E> implements List<E> {public Iterator<E> iterator() {return new Itr();}private class Itr implements Iterator<E> {...}public ListIterator<E> listIterator() {return new ListItr(0);}private class ListItr extends Itr implements ListIterator<E> {public void add(E e) {checkForComodification();try {int i = cursor;ArrayList.this.add(i, e);cursor = i + 1;lastRet = -1;expectedModCount = modCount;} catch (IndexOutOfBoundsException ex) {throw new ConcurrentModificationException();}}} }

12_增強for循環

2.5、增強for循環

增強for:簡化數組和Collection集合的遍歷

  • 實現Iterable接口的類允許其對象成為增強型 for語句的目標。
  • 它是JDK5之后出現的,其內部原理是一個Iterator迭代器。

增強for的格式:

? ? for(元素數據類型 變量名 : 數組或者Collection集合) {
? ? ? ? // 循環體; // 在此處使用變量即可,該變量就是元素
? ? }

范例

? ? int[] arr = {1, 2, 3, 4, 5};
? ? for(int i: arr) {
? ? ? ? System.out.println(i);
? ? }

13_List集合存儲學生對象三種方式遍歷

案例:List集合存儲學生對象用三種方式遍歷

14_數據結構之棧和隊列

2.6、數據結構

數據結構是計算機存儲、組織數據的方式。是指相互之間存在一種或多種特定關系的數據元素的集合。

通常情況下,精心選擇的數據結構可以帶來更高的運行或者存儲效率。

2.7、常見數據結構之棧

2.8、常見數據結構之隊列

15_數據結構之數組和鏈表

2.9、常見數據結構之數組

2.10、常見數據結構之鏈表

16_List集合子類的特點

2.11、List集合子類特點

List集合常用子類:ArrayList、LinkedList

ArrayList? 集合:底層是數組結構實現,查詢快、增刪慢?。

LinkedList集合:底層是鏈表結構實現,查詢慢、增刪快?。

?

練習:分別使用ArrayList和LinkedList完成存儲字符串并遍歷。

?

package com.itheima_07;import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList;public class ListDemo {public static void main(String[] args) {//創建集合對象ArrayList<String> arrayList = new ArrayList<String>();arrayList.add("hello");arrayList.add("world");arrayList.add("java");// 1、迭代器:集合特有的遍歷方式Iterator<String> iterator = arrayList.iterator();while (iterator.hasNext()) {System.out.println(iterator.next());}System.out.println("----------------");// 2、普通for:帶有索引的遍歷方式for (int i = 0; i < arrayList.size(); i++) {System.out.println(arrayList.get(i));}System.out.println("----------------");// 3、增強for:最方便的遍歷方式for (String s : arrayList) {System.out.println(s);}System.out.println("----------------");LinkedList<String> linkedList = new LinkedList<String>();linkedList.add("hello2");linkedList.add("world2");linkedList.add("java2");// 1、迭代器:集合特有的遍歷方式Iterator<String> iterator2 = linkedList.iterator();while (iterator2.hasNext()) {System.out.println(iterator2.next());}System.out.println("--------");// 2、普通for:帶有索引的遍歷方式for (int i = 0; i < linkedList.size(); i++) {System.out.println(linkedList.get(i));}System.out.println("--------");// 3、增強for:最方便的遍歷方式for (String s : linkedList) {System.out.println(s);}System.out.println("--------");} }

17_ArrayList集合存儲學生對象三種方式遍歷

案例:ArrayList集合存儲學生對象,用三種方式遍歷

18_LinkedList集合的特有功能

2.12、LinkedList集合的特有功能

總結

以上是生活随笔為你收集整理的Java14-day05【集合(Collection常用方法-遍历、List特有方法、List集合子类特点、LinkedList集合的特有功能、ListIterator)】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。