简体   繁体   English

比较 ArrayList 中的两个字符串

[英]Comparing two strings in ArrayList

I got problems with comparing two strings in my loop.我在循环中比较两个字符串时遇到问题。

Say I want to put two words in an ArrayList.假设我想在 ArrayList 中放入两个单词。 I decide write both words through inputDialogeBox and put them in the list.我决定通过 inputDialogeBox 写两个单词并将它们放在列表中。 If the two words are the same the second word will not be added to the list.如果两个词相同,则不会将第二个词添加到列表中。

But when I compare the two strings the program doesnt seem to care if they are identical and both are ending up in the list.但是当我比较这两个字符串时,程序似乎并不关心它们是否相同并且都在列表中。

Heres my code:这是我的代码:

package testing;

import javax.swing.*;

public class Testing {

    public static void main(String[] args) {
        Word c = new Word();

        // word 1
        String word;
        word = JOptionPane.showInputDialog("Write a word: ");
        System.out.println("word1 = " + word);
        c.setWord(word);

        // word 2
        word = JOptionPane.showInputDialog("Write a new word: ");
        System.out.println("word2 = " + word);
        c.setWord(word); // sätt kortet≤

        System.out.println("words = " + c.getWord().size() + " " + c.getWord());
    }
}

And my class:还有我的班级:

package testing;

import java.util.ArrayList;

public class Word {

    ArrayList<String> words = new ArrayList<>();

    public void setWord(String word) {
        // set first value in arraylist to start the loop
        if (words.isEmpty()) {
            words.add("default");
        }

        for (int i = 0; i < words.size(); i++) {
            if (!words.get(i).equals(word)) {
                words.add(word);
                System.out.println("words.get(i): " + words.get(i) + "  word: " + word);
                break;
            }
        }
    }

    public ArrayList getWord() {
        return words;
    }
}

My guess is that the problem is around I add one defaultvalue, just to get something to loop with.我的猜测是问题在于我添加了一个默认值,只是为了得到一些循环。 Otherwise if the ArrayList is empty I cannot start my loop.否则,如果 ArrayList 为空,我将无法开始我的循环。 But maybe there is a better way?但也许有更好的方法?

Thanks谢谢

The issue is that you are not checking for all of the values, only one by one.问题是你没有检查所有的值,只是一个一个地检查。 For the first word, you check against 'default' and add to the list.对于第一个单词,您检查“默认”并添加到列表中。 For the second word, you check against 'default' first, and since this is not equal, you add that word to the list also.对于第二个单词,您首先检查“default”,由于这不相等,因此您也将该单词添加到列表中。

public void setWord(String word) {

    boolean exists = false;
    for (int i = 0; i < words.size(); i++) {
        if (words.get(i).equals(word)) {
           exists = true;
           break;
        }
    }
    if (!exists) {
        words.add(word);
        System.out.println("words.get(i): " + words.get(i) + "  word: " + word);
    }
}

or you could use ArrayList contains:或者您可以使用 ArrayList 包含:

public void setWord(String word) {

    if(!words.contains(word)) {
        words.add(word);
        System.out.println("words.get(i): " + words.get(i) + "  word: " + word);
    }
}

In these type of cases you should use Set instead of ArrayList.在这些类型的情况下,您应该使用 Set 而不是 ArrayList。 Still below code modification should help you.仍然在下面的代码修改应该对你有所帮助。

public void setWord(String word) {'
        boolean found = false;    
        for (int i = 0; i < words.size(); i++) {
            if (words.get(i).equals(word)) {
                found=true;
                break;
            }
        }
        if(!found){
           words.add(word);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM