比较 Java 中的两个 ArrayList<String> 列表

Comparting two ArrayList<String> lists in Java

好的...所以我一直在尝试做的是比较两个列表:words 和 d。他们共有的词需要添加到第三个列表:realWords.

词典只是另一个 class 中的一个列表,里面有一堆单词。

List<String> realWords = new ArrayList<String>();
Dictionary d = new Dictionary();

这些是我已经尝试过但没有奏效的方法("worked" 我的意思是输出什么也没有,没有错误):

尝试 1

List<String> realWords = new ArrayList<String>(words);
    realWords.retainAll(d);

尝试 2

for(int i = 0; i < words.size(); i++) {
        if (d.contains(words.get(i))){      
            realWords.add(words.get(i));
        }
    }

尝试 3

for(String word : words) {
        if (d.contains(word)) {
            realWords.add(word);
        }
    }

尝试 4

for (int i = 0; i < words.size(); i++) {
        for (int j = 0; i < d.size(); i++) {
            if(words.get(i) == d.get(j)) {
                realWords.add(words.get(i));
            }       
        }
    }

然后在我遗漏的那部分代码之后:

return realWords;

提前致谢!

Edit1:Dictionary.java 的代码是:

package a1;

import java.util.*;

public class Dictionary extends ArrayList<String> {

    public Dictionary() {

        this.add("abalone");
        this.add("abandon");
        this.add("abashed");
        this.add("abashes");
        this.add("abasing");
        this.add("abating");
        this.add("abdomen");
        this.add("abducts");
        this.add("abetted");
        this.add("abetter");
        this.add("abettor");
        this.add("abiding");
        this.add("ability");
        this.add("abjured");
        this.add("abjures");
        this.add("abolish");
        this.add("aborted");
        this.add("abounds");
        this.add("abraded");
        // and then more words

        }
    }

注意:此代码已提供给我们,无法更改。

您可以使用增强的 for 循环

   List<String> realWords = new ArrayList<>();
   Dictionary d = new Dictionary();
   List<String> words = new ArrayList<String>();
    words.add("abetted");
    words.add("ability");


    for (String word : words) {
        if (d.contains(word)) {
            realWords.add(word);
        }
    }

    System.out.println(realWords);
}
}

以下代码有如下输出:[abalone, abolish] 问题一定出在其他地方。您确定 "words" 包含有效单词吗?尝试输出并手动检查。

public class Example {
    public static class Dictionary extends ArrayList<String> {

        public Dictionary() {

            this.add("abalone");
            this.add("abandon");
            this.add("abashed");
            this.add("abashes");
            this.add("abasing");
            this.add("abating");
            this.add("abdomen");
            this.add("abducts");
            this.add("abetted");
            this.add("abetter");
            this.add("abettor");
            this.add("abiding");
            this.add("ability");
            this.add("abjured");
            this.add("abjures");
            this.add("abolish");
            this.add("aborted");
            this.add("abounds");
            this.add("abraded");
            // and then more words

        }
    }

    public static void main(String[] args) {
        List<String> realWords = new ArrayList<String>(
                Arrays.asList("abalone", "foobar", "abolish"));
        Dictionary d = new Dictionary();
        realWords.retainAll(d);
        System.out.println(realWords);
    }

}

编辑:您的其他尝试在功能上是相同的,它们具有相同的复杂性 (O(words.size() * d.size()) 但更长且更少 obvious/clear.

代码运行良好,请调试您的代码。请参阅输出以供参考。

public static void main(String[] args) {
        List<String> words = new ArrayList<String>();
        words.add("abashes");
        words.add("sdqad");
        words.add("abducts");
        words.add("sadadads");

        List<String> realWords = new ArrayList<String>();
        Dictionary d = new Dictionary();

        for (int i = 0 ; i < words.size() ; i++) {
            if (d.contains(words.get(i))) {
                realWords.add(words.get(i));
            }
        }

        System.out.println(realWords);
    }

输出

[abashes, abducts]

我建议使用 Java 8 个流:

List<Strings> realWords = words.stream()
    .filter(d::contains).collect(Collectors.toList());