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 并删除匹配的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Reliable implementation of PBKDF2-HMAC-SHA256 for JAVA(PBKDF2-HMAC-SHA256 for JAVA 的可靠实现)
Correct way to sign and verify signature using bouncycastle(使用 bouncycastle 签名和验证签名的正确方法)
Creating RSA Public Key From String(从字符串创建 RSA 公钥)
Why java.security.NoSuchProviderException No such provider: BC?(为什么 java.security.NoSuchProviderException 没有这样的提供者:BC?)
Generating X509 Certificate using Bouncy Castle Java(使用 Bouncy Castle Java 生成 X509 证书)
How can I get a PublicKey object from EC public key bytes?(如何从 EC 公钥字节中获取 PublicKey 对象?)