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

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

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

問題描述

基本上,用戶提交一個字符串,迭代器在 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

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

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

相關(guān)文檔推薦

Java Remove Duplicates from an Array?(Java從數(shù)組中刪除重復(fù)項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復(fù)調(diào)用失敗來自服務(wù)器的意外響應(yīng):在 Android 工作室中未經(jīng)授權(quán))
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夜夜 | 精品国产一区二区三区观看不卡 | 91麻豆精品国产91久久久更新资源速度超快 | 伊人手机在线视频 | 国产成人99久久亚洲综合精品 | 亚洲高清av | 久久久99国产精品免费 | 青娱乐av| 蜜臀久久99精品久久久久久宅男 | 91免费小视频 | 麻豆av电影网 | 久久久一二三区 | av午夜电影 | 成人福利电影 | 91亚洲国产成人久久精品网站 | 91精品无人区卡一卡二卡三 | 成人久久18免费网站麻豆 | 亚洲精品美女在线观看 | 国产精品一区一区 | 欧美一区二区三区视频在线播放 | 色伊人| 91免费视频 | 一级黄色日本片 | 丝袜美腿一区二区三区动态图 | 久久久www成人免费精品张筱雨 | 在线国产视频 | 九九亚洲 | 国产精品69久久久久水密桃 | 四虎精品在线 | 日韩中文字幕免费在线 | 亚洲电影免费 | 一区二区三区精品视频 | 国产一区二区精品在线 | 免费v片| 国产精品成人久久久久a级 久久蜜桃av一区二区天堂 | 在线播放中文字幕 | 中文字幕乱码一区二区三区 | 国产一区在线视频 | 一区二区在线视频 |