最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

java迭代器iterator

2022-07-27 18:27 作者:虛云幻仙  | 我要投稿

/**
* iterator迭代器 Collection接口繼承了Iterable接口 iterable可迭代的 在Iterable接口中定義了iterator()方法用于生成迭代器
* Collection接口的所有實現(xiàn)類都實現(xiàn)了iterator()方法,可以通過迭代器來對Collection容器進行遍歷
*/

public class TestIterator1 {
? ?public static void main(String[] args) {
? ? ? ?LinkedList<String> ll1= new LinkedList<>();
? ? ? ?ll1.addFirst("a");
? ? ? ?ll1.addLast("b");
? ? ? ?ll1.addLast("c");
? ? ? ?for (int i = 0;i<ll1.size();i++){
? ? ? ? ? ?System.out.print(ll1.get(i));
? ? ? ?}
? ? ? ?//利用index遍歷
? ? ? ?System.out.println();
? ? ? ?for (String l :
? ? ? ? ? ? ? ?ll1) {
? ? ? ? ? ?System.out.print(l);
? ? ? ?}
? ? ? ?//利用for-each遍歷
? ? ? ?System.out.println();

? ? ? ?Iterator<String> it1 = ll1.iterator();
? ? ? ?//.iterator()方法生成一個新的迭代器對象并返回 ? 迭代器類型為Iterator接口的實現(xiàn)類 ? ?迭代器使用游標來實現(xiàn)迭代的操作
? ? ? ?//Iterator<E>泛型接口 將泛型設為元素的類型

? ? ? ?while(it1.hasNext()){
? ? ? ? ? ?//Iterator接口的方法.hasNext() ? 判斷游標當前指向的位是否為有元素 ? ?有元素true null則false
? ? ? ? ? ?// 在集合中最后一個元素的下一位為null ? ? 指向null即完成了集合的遍歷

? ? ? ? ? ?System.out.print(it1.next());
? ? ? ? ? ?//Iterator接口的方法.next() ?返回游標當前指向的元素并將游標指向下一位 ? ?相當于同時完成了.get(i)和i++兩個操作
? ? ? ? ? ?it1.remove();
? ? ? ? ? ?//移除.next()返回的元素 ? ?.remove()方法在.next()方法后面只能使用一次
? ? ? ?}
? ? ? ?//結果為abc
? ? ? ?System.out.println();
? ? ? ?System.out.println(ll1);
? ? ? ?//容器結果為[] ?所有元素都被移除了

? ? ? ?ll1.add("d");
? ? ? ?ll1.add("e");
? ? ? ?ll1.add("f");
? ? ? ?for (Iterator<String> it = ll1.iterator();it.hasNext();){
? ? ? ? ? ?//迭代器為一次性的 ?每次游標移至null無法再使用 ? ? 可以通過.iterator()再生成一個新的迭代器
? ? ? ? ? ?//將迭代器的聲明放在for循環(huán)中,循環(huán)條件為.hasNext() ? 迭代因子空置因為.next()已經(jīng)實現(xiàn)了

? ? ? ? ? ?String e = it.next();
? ? ? ? ? ?System.out.print(e);
? ? ? ?}
? ? ? ?//結果為def
? ? ? ?System.out.println();
? ? ? ?//所有實現(xiàn)了Iterable的集合使用iterator的方法都是一樣的,Set實現(xiàn)類也通過如上操作使用迭代器
? ? ? ?//Map接口沒有繼承Iterable,不能直接使用迭代器,但Map接口提供了.keySet()和.entrySet()方法,兩種方法都返回set集合,可以通過set集合的迭代器對Map間接遍歷


? ? ? ?Map<String,Integer> hm = new HashMap<>();
? ? ? ?hm.put("zhang",59);
? ? ? ?hm.put("zhao",61);
? ? ? ?hm.put("li",99);
? ? ? ?Set<String> nameSet = hm.keySet();
? ? ? ?Iterator<String> it2 = nameSet.iterator();
? ? ? ?//通過map集合生成所有key的集合,用過key的集合生成集合的迭代器
? ? ? ?while (it2.hasNext()){
? ? ? ? ? ?String name = it2.next();
? ? ? ? ? ?//拿到的name即map的key
? ? ? ? ? ?System.out.print(name+"="+hm.get(name)+",");
? ? ? ? ? ?//通過.get(key)返回value
? ? ? ?}
? ? ? ?//結果為zhao=61,zhang=59,li=99,
? ? ? ?System.out.println();

? ? ? ?Set<Map.Entry<String,Integer>> entries = hm.entrySet();
? ? ? ?//.entrySet()返回的是Set集合,集合的泛型為<Map.Entry ? ? Entry是Map的內(nèi)部接口,需要通過Map.調(diào)用 ? Map.Entry的泛型為<String,Integer> ? 所以需要套兩層泛型
? ? ? ?for (Iterator<Map.Entry<String,Integer>> it = entries.iterator();it.hasNext();){
? ? ? ? ? ?//迭代器的類型為Iterator<>,是Set集合的迭代器,集合內(nèi)裝的元素為Map.Entry ? ?所以迭代器的泛型同樣為<Map.Entry<String,Integer>>
? ? ? ? ? ?Map.Entry<String,Integer> entry = it.next();
? ? ? ? ? ?System.out.print(entry.getKey()+"="+entry.getValue()+",");
? ? ? ? ? ?//需要分別調(diào)用getKey和getValue,如果直接打印it.next().getKey()+it.next().getValue()會打印當前key和下一個value
? ? ? ?}
? ? ? ?//結果為zhao=61,zhang=59,li=99,
? ? ? ?System.out.println();

? ?}
}

class TestRemove{
? ?//容器的刪除操作
? ?public static void main(String[] args) {
? ? ? ?List<Integer> arr = new ArrayList<>();
? ? ? ?arr.add(0);
? ? ? ?arr.add(1);
? ? ? ?arr.add(2);
? ? ? ?arr.add(3);
? ? ? ?for (int i = 0;i<arr.size();i++){
? ? ? ? ? ?arr.remove(2);
? ? ? ? ? ?System.out.println(arr);
? ? ? ?}
? ? ? ?//結果為[0, 1, 3] ? [0, 1]
? ? ? ?//迭代器以外的循環(huán)遍歷中不可以刪除元素,當i=0時刪除index=2,后面的元素3前移,size-1,i=1時再次刪除index=2,size變?yōu)?,如果原始容器為0,1,2的話在第二次循環(huán)時index=2=size索引越界會拋異常
? ? ? ?//這里如果循環(huán)條件沒有使用.size()而直接使用長度4的話當size變小時會出現(xiàn)i>size的情況同樣會索引越界


? ? ? ?arr.add(5);
? ? ? ?arr.add(6);
? ? ? ?arr.add(7);
? ? ? ?arr.add(5);
? ? ? ?arr.add(8);
? ? ? ?arr.add(5);
? ? ? ?System.out.println(arr+"[0, 1, 5, 6, 7, 5, 8, 5]");
? ? ? ?while (arr.remove(Integer.valueOf(5))){
? ? ? ? ? ?//利用.remove(Object o)方法刪除元素返回boolean當做循環(huán)條件 ?當返回true即刪除了一個5,當返回false即容器中已經(jīng)沒有元素5了
? ? ? ? ? ?//注意List容器還有.remove(int index)方法返回E element 元素5不能直接寫數(shù)字,需要轉(zhuǎn)為包裝類才能被當做元素

? ? ? ? ? ?System.out.println(arr);
? ? ? ?}
? ? ? ?//結果為
? ? ? ?//[0, 1, 6, 7, 5, 8, 5]
? ? ? ?//[0, 1, 6, 7, 8, 5]
? ? ? ?//[0, 1, 6, 7, 8]
? ? ? ?//如果將遍歷和刪除操作放在一個循環(huán)語句內(nèi)執(zhí)行,會出現(xiàn)索引越界或者遍歷跳過部分元素的情況


? ? ? ?List<Integer> ind1 = new ArrayList<>();
? ? ? ?//創(chuàng)建一個容器用于存儲要刪除的元素的索引
? ? ? ?for (int i = 0;i<arr.size();i++){
? ? ? ? ? ?if (arr.get(i)==6){
? ? ? ? ? ? ? ?ind1.add(i);
? ? ? ? ? ? ? ?//當元素為6時將索引記錄下來
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?for (int i =0;i<ind1.size();i++){
? ? ? ? ? ?arr.remove(ind1.get(i));
? ? ? ? ? ?//ind1.get(i)返回存儲的要刪除的索引值,在arr容器遍歷循環(huán)之外可以安全刪除元素
? ? ? ?}

// ? ? ? ?for (int i = 0;i<arr.size();i++){
// ? ? ? ? ? ?while (arr.get(i)==8){
// ? ? ? ? ? ? ? ?arr.remove(i);
// ? ? ? ? ? ?}
// ? ? ? ? ? ?System.out.println(arr.get(i));
// ? ? ? ? ? ?//報錯 索引越界 元素8為容器中最后一位 刪除8之后當前索引i已經(jīng)越界,當調(diào)用get(i)時拋出異常
// ? ? ? ?}
? ? ? ?//刪除操作容易造成索引越界

? ? ? ?System.out.println(arr+"[0, 1, 6, 7, 8]");
? ? ? ?List<String> str = new ArrayList<>();
? ? ? ?str.add("a");
? ? ? ?str.add("b");
? ? ? ?str.add("c");
? ? ? ?str.add("d");
// ? ? ? ?for (String s :
// ? ? ? ? ? ? ? ?str) {
// ? ? ? ? ? ?if ("b".equals(s)) {
// ? ? ? ? ? ? ? ?str.remove(s);
// ? ? ? ? ? ?}
// ? ? ? ?}
? ? ? ?/* ?報異常
? ? ? ?Exception in thread "main" java.util.ConcurrentModificationException
? ? ? ? ? ?at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:911)
? ? ? ? ? ?at java.util.ArrayList$Itr.next(ArrayList.java:861)
? ? ? ? ? ?at TestRemove.main(TestIterator1.java:148)
? ? ? ? ? ?//if (modCount != expectedModCount) {throw new ConcurrentModificationException();}
? ? ? ? ? ?//modCount修改計數(shù)器 expected預期的 concurrent同時發(fā)生的/并發(fā) ?modification修改
? ? ? ? ? ?//for-each遍歷中無法刪除元素 也無法add添加元素 ?ArrayList$Itr是ArrayList類的$內(nèi)部類Itr
? ? ? ? ? ?//private class Itr implements Iterator<E> for-each是使用迭代器iterator實現(xiàn)遍歷
? ? ? ? */


? ? ? ?Iterator<String> iter1 = str.iterator();
? ? ? ?while (iter1.hasNext()){
? ? ? ? ? ?String e = iter1.next();
? ? ? ? ? ?if ("d".equals(e)){
? ? ? ? ? ? ? ?iter1.remove();
? ? ? ? ? ? ? ?//.remove()方法刪除next方法返回的元素,而非next方法調(diào)整游標后移之后游標指向的元素
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?System.out.println(str);
? ? ? ?//結果為[a, b, c]

// ? ? ? ?for (Iterator<String> iter2 = str.iterator();iter2.hasNext();){
// ? ? ? ? ? ?if ("c".equals(iter2.next())){
// ? ? ? ? ? ? ? ?str.add("d");
// ? ? ? ? ? ?}
// ? ? ? ?}
? ? ? ?//報異常ConcurrentModificationException ? ?使用迭代器遍歷時無法添加元素 ?for-each使用迭代器實現(xiàn)所以遍歷時也無法添加元素
? ?}
}

java迭代器iterator的評論 (共 條)

分享到微博請遵守國家法律
荣昌县| 天祝| 永新县| 静乐县| 东阿县| 普洱| 新河县| 务川| 遵义市| 恩平市| 云龙县| 绥宁县| 六盘水市| 增城市| 九龙坡区| 怀远县| 岳普湖县| 莎车县| 沅江市| 古丈县| 内江市| 遂昌县| 丘北县| 宿迁市| 子长县| 株洲市| 宁陵县| 马鞍山市| 富平县| 伽师县| 合水县| 大田县| 岱山县| 丹巴县| 邢台县| 诏安县| 长丰县| 巴林左旗| 茌平县| 高邮市| 汨罗市|