集合為什么要有迭代器,集合框架(List、Collection、迭代器)

 2023-10-06 阅读 29 评论 0

摘要:介紹集合中的特有方法【Collection and List】 攻城獅搜索工具 在線文檔jdk Collection接口—搜索順序 點擊鏈接點java.util點 Collection<E 集合為什么要有迭代器。其中重點方法有: ------------------------------------------------------------Iterator--------

介紹集合中的特有方法【Collection and List】

攻城獅搜索工具
在線文檔jdk

Collection接口—搜索順序

  1. 點擊鏈接
  2. 點java.util
  3. 點 Collection<E

集合為什么要有迭代器。其中重點方法有:
------------------------------------------------------------Iterator---------------------------------------------------------
優點:返回在此collection的元素上進行迭代的迭代器
不足:關于元素返回的順序沒有任何保證(除非此 collection 是某個能提供保證順序的類實例)

public class CollectionDemo {public static void main(String[] args) {ArrayList al = new ArrayList<>();al.add("a");al.add("b");al.add("c");al.add("d");al.add("e");//集合List三種遍歷方式① for(Object obj : al) {System.out.println(obj);}② for (int i = 0; i < al.size(); i++) {System.out.println(al.get(i)); }③ Iterator it = al.iterator();while(it.hasNext()){System.out.println(it.next());}}
}

集合的remove方法與迭代器的remove方法有啥區別?
例子①:刪除容器里的所有元素 錯誤代碼演示

public class CollectionDemo {public static void main(String[] args) {ArrayList al = new ArrayList<>();al.add(22);al.add(24);al.add(26);  al.add(28);al.add(29);Iterator it = al.iterator();while(it.hasNext()){Object obj=it.next();al.remove(obj);}}
}

顯示臺會出現 :java.util.ConcurrentModificationException
表示:這段代碼出現了并發問題,同志們一定要注意了

迭代器是一種范型、迭代器在內存中的走向 如下圖:

在這里插入圖片描述
上圖代碼如下:

public class CollectionDemo {public static void main(String[] args) {ArrayList al = new ArrayList<>();al.add(22);al.add(24);al.add(26);al.add(28);al.add(29);Iterator it = al.iterator();while(it.hasNext()){int obj=(Integer)it.next();if(obj % 2==0) {System.out.println(it.next());}}

學集合框架就是為了了解容器的數據結構

list集合排序?---------------------------------------------------ArrayList-----------------------------------------------------
數組結構:

增刪慢,查詢快,有連續下標 線程不同步<沒有鎖旗標> 增長因子為1.5 10

---------------------------------------------------------vector--------------------------------------------------------
數組結構:

增刪改查都慢,有連續下標 線程同步<有鎖旗標,一個用戶操作一個方法> 增長因子為2 10

------------------------------------------------------Linkedlist----------------------------------------------------
鏈表結構如圖:
在這里插入圖片描述

增刪快,查詢慢,沒有連續下標。如圖若想問第一個A認識D嗎 需要通過BC才能問到,所以查詢慢
假如說C是員工,想把C刪除,就只要通過BD兩個老板就那能夠把C刪除,所以增刪快

迭代器是取出集合元素的方式、特有方法:

  1. addFirst():在最前面增加
  2. addLast():在最后面增加
  3. getFirst():獲取第一個
  4. getLast():獲取第二個
  5. removeFirst():移除第一個
  6. removeLast():移除第二個

相關的面試題
①利用Linkedlist完成具有堆棧特性的容器

什么是堆棧?如圖:
假如有三個字母A/B/C依次分別放入兩個容器內

集合鏈表?在這里插入圖片描述
①的解題思路:

  • 定義一個容器 Linkedlist
  • 堆棧的存放特點:每一次都將元素放入容器的頂部
  • 堆棧的取出特點,每一次都是從容器的頂部取出元素

①的解答代碼如下:

public class LinkedListDemo {public static void main(String[] args) {DuiZhan dz=new DuiZhan();dz.push("a");dz.push("b");dz.push("c");dz.bianli();}
}//定義一個容器class DuiZhan{private LinkedList ll=new LinkedList<>();//定義一個存放的方法public void push(Object obj){ll.addFirst(obj); //先往第一個加東西}public Object pop(){return ll.removeFirst();//由于堆棧是先進后出的,所以移除第一個,也就是最上面一個}//然后再把元素遍歷出來public void bianli() {Iterator it=ll.iterator();while(it.hasNext()) {System.out.println(it.next());}}}

②利用Linkedlist完成具有列隊特性的容器
隊列的話就把堆棧的那種第一個追加改成最后一個是追加

-------------------------------------------------------------set-------------------------------------------------------------
特點:

無序,元素不可以重復

-------------------------------------------------------------list-------------------------------------------------------------
特點:

有序,元素可以 重復 because該集合體系有索引
List里存放的對象是有序的,同時也是可以重復的,List關注的是索引,
擁有一系列和索引相關的方法,查詢速度快。因為往list集合里插入或刪除數據時,
會伴隨著后面數據的移動,所有插入刪除數據速度慢。

**父類有的方法子迭代器一樣有 例如:
①ListIterator :順著取
**

public class ListDemo {public static void main(String[] args) {ArrayList al = new ArrayList<>();al.add(22);al.add(24);al.add(26);al.add(28);al.add(29);ListIterator it=al.listIterator();while(it.hasNext()) {System.out.println(it.next());}}

②hasPrevious:倒著取

public class ListDemo {public static void main(String[] args) {ArrayList al = new ArrayList<>();al.add(22);al.add(24);al.add(26);al.add(28);al.add(29);ListIterator it=al.listIterator();while(it.hasPrevious()) {System.out.println(it.previous());   } }}

-----------------------------------------------List實現類vector---------------------------------------------------
對于Vector只需了解他自己有的特有的遍歷方式

public class VectorDemo {public static void main(String[] args) {Vector al=new Vector<>();al.add(22);al.add(24);al.add(26);al.add(28);al.add(29);Enumeration elements = al.elements(); //拿到枚舉對象while(elements.hasMoreElements()) {System.out.println(elements.nextElement());//在枚舉里面拿到它的下一個元素}
}
}

集合框架ArrayList中的重復元素去重以及其底層原理
*如何去除list集合中的重復元素

ArrayList集合去重復代碼如下:

方法①

public class ArrayListRepeatDemo {public static void main(String[] args) {  ArrayList al=new ArrayList<>(); al.add("a");al.add("b");al.add("c");al.add("a"); ArrayList newAll=repeatList(al); System.out.println(newAll.size()); } private static ArrayList repeatList(ArrayList al) {ArrayList newAll=new ArrayList<>();for (Object obj : al) {if(!newAll.contains(obj)) {newAll.add(obj);}}return newAll;}
}

方法②

public class ArrayListRepeatDemo {public static void main(String[] args) {ArrayList al=new ArrayList<>();
//自定義的對象  	al.add(new Person("a","18"));al.add(new Person("b","15"));al.add(new Person("c","19"));al.add(new Person("a","18"));ArrayList newAll=repeatList(al);System.out.println(newAll.size());}private static ArrayList repeatList(ArrayList al) {ArrayList newAll=new ArrayList<>();for (Object obj : al) {if(!newAll.contains(obj)) {newAll.add(obj);}}return newAll;}
}class Person{private String name;private String age;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}@Overridepublic String toString() {return "Person [name=" + name + ", age=" + age + "]";}public Person() {super();}public Person(String name, String age) {super();this.name = name;this.age = age;}@Overridepublic boolean equals(Object obj) {if(obj instanceof Person) {Person p=(Person)obj;System.out.println(this.getName()+"---equals---"+p.getName());return this.getName().equals(p.getName()) && this.getAge()==p.getAge();}return false;}
}

重點總結:

①、集合的remove方法與迭代器的remove方法有什么區別,容易出現什么異常?
并發異常 兩個對象對同一個容器進行操作

②、ArrayList與Array的區別?
Array是數組:長度在數組申明時候,就已經確定了;存放的元素也確定了;
ArrayList:長度是可變的;存放元素隨意

③、ArrayList與Linkedlist的區別?
ArrayList:數據結構不同、數組、查詢快、增刪慢、增長因子
Linkedlist:鏈表結構、查詢慢、增刪快、addFirst

④、list集合去重的思路,涉及到的方法有哪些,針對于引用數據?
假設有一個容器,里面存放了5個對象,其中有一個對象是重復的?
a 定義去重的方法(定義一個新的容器,判斷新的容器是否包含指定的元素,如果包含,那么不添加新容器中,如果不包含,那么添加到新容器中)
b String之所以能夠去重,是因為重寫Object的equals方法
c 那么自定義的對象需要去去重的話,那么也就需要重寫Object的equals方法

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/120895.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息