久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Java,使用迭代器搜索 ArrayList 并刪除匹配的對象

Java, Using Iterator to search an ArrayList and delete matching objects(Java,使用迭代器搜索 ArrayList 并刪除匹配的對象)
本文介紹了Java,使用迭代器搜索 ArrayList 并刪除匹配的對象的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

基本上,用戶提交一個字符串,迭代器在 ArrayList 中搜索該字符串.找到后,迭代器將刪除包含該字符串的對象.

Basically, the user submits a String which the Iterator searches an ArrayList for. When found the Iterator will delete the object containing the String.

因為這些對象中的每一個都包含兩個字符串,所以我很難將這些行寫成一個.

Because each of these objects contain two Strings, I am finding trouble writing these lines as one.

Friend current = it.next();
String currently = current.getFriendCaption();

感謝您的幫助!

推薦答案

你不需要它們在一行,只要在匹配時使用 remove 刪除一個項目:

You don't need them on one line, just use remove to remove an item when it matches:

Iterator<Friend> it = list.iterator();
while (it.hasNext()) {
    if (it.next().getFriendCaption().equals(targetCaption)) {
        it.remove();
        // If you know it's unique, you could `break;` here
    }
}

完整演示:

import java.util.*;

public class ListExample {
    public static final void main(String[] args) {
        List<Friend>    list = new ArrayList<Friend>(5);
        String          targetCaption = "match";

        list.add(new Friend("match"));
        list.add(new Friend("non-match"));
        list.add(new Friend("match"));
        list.add(new Friend("non-match"));
        list.add(new Friend("match"));

        System.out.println("Before:");
        for (Friend f : list) {
            System.out.println(f.getFriendCaption());
        }

        Iterator<Friend> it = list.iterator();
        while (it.hasNext()) {
            if (it.next().getFriendCaption().equals(targetCaption)) {
                it.remove();
                // If you know it's unique, you could `break;` here
            }
        }

        System.out.println();
        System.out.println("After:");
        for (Friend f : list) {
            System.out.println(f.getFriendCaption());
        }

        System.exit(0);
    }

    private static class Friend {
        private String friendCaption;

        public Friend(String fc) {
            this.friendCaption = fc;
        }

        public String getFriendCaption() {
            return this.friendCaption;
        }

    }
}

輸出:

$ java ListExample 
Before:
match
non-match
match
non-match
match

After:
non-match
non-match

這篇關于Java,使用迭代器搜索 ArrayList 并刪除匹配的對象的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 中文字幕在线一区二区三区 | 成人午夜毛片 | 国产黄色在线观看 | 成年无码av片在线 | 成人av免费| 亚洲精品视频一区二区三区 | 欧美日韩在线一区二区三区 | 国产精品亚洲精品日韩已方 | 欧美亚洲一区二区三区 | 亚洲国产欧美一区 | 麻豆av网站| 成人午夜视频在线观看 | 91精品国产91久久久久久吃药 | 婷婷激情在线 | 免费久久久| 99re视频在线 | 日韩一区二区三区视频在线观看 | 99久久久国产精品免费消防器 | 欧美一区二区二区 | 成av人电影在线 | 欧美午夜精品久久久久久浪潮 | 狠狠干狠狠插 | 国精日本亚洲欧州国产中文久久 | www国产亚洲精品久久网站 | 国产精品区一区二区三区 | 农村真人裸体丰满少妇毛片 | 欧美专区在线 | 一区二区三区在线电影 | 欧美日韩久久久 | 伊人久久综合 | 日日天天| 亚洲国产精品视频 | 久久亚洲国产精品日日av夜夜 | 国产一区二区在线观看视频 | 亚洲高清免费 | 久久久www成人免费精品 | 日韩精品一区二区三区中文字幕 | 日韩精品激情 | 日本在线观看视频 | 精品国产欧美一区二区 | 一级毛片免费看 |