在一个ArrayList中查找重复项(JFrame)

huangapple go评论81阅读模式
英文:

Finding duplicates in an ArrayList(JFrame)

问题

我正在尝试创建一个Swing应用程序,它会将名称添加到一个ArrayList,然后在JComboBox中显示出来。我已经创建了窗口和其他内容,但是我似乎无法掌握检测重复名称的方法。

我尝试了以下代码:

btnnext.addActionListener(new ActionListener() { 
    @Override
    public void actionPerformed(ActionEvent e) {
    
        if(checkDuplicate(names)==true)
        {
            names.add(txtname.getText());
            txtname.setText("");
        }
        else {
            JOptionPane.showMessageDialog(null,"重复!不要添加");
        }
    }
});


public static boolean checkDuplicate(ArrayList<String> list) {
    HashSet<String> set = new HashSet<>();
    for (int i = 0; i < list.size(); i++) {
        boolean val = set.add(list.get(i));
        if (!val) {
            return val;
        }
    }
    return true;
}

当我将名称添加到ArrayList中后,它只会在我已经添加了的情况下显示重复,当我收到消息后,无法再添加其他任何内容。

输入示例:

test

test


然后它停止接受新的字符串,并且只显示消息“重复!不要添加”。

英文:

I'm trying to do a swing application which adds names to an ArrayList and then displays it in Jcombobox.I already did the window and everything but I can't seem to get the hang off detecting duplicate names.

I tried

    btnnext.addActionListener(new ActionListener() { 
    Override
    public void actionPerformed(ActionEvent e) {
    
    if(checkDuplicate(names)==true)
    {
    names.add(txtname.getText());
    txtname.setText(&quot;&quot;);
    }
    else {
    
    JOptionPane.showMessageDialog(null,&quot;DUPLICATE! do not add&quot;);
    }
    }
    });
    
    
    
    public static boolean checkDuplicate(ArrayList&lt;String&gt; list) {
    HashSet set = new HashSet();
    for (int i = 0; i &lt; list.size(); i++) {
    boolean val = set.add(list.get(i));
    if (val == false) {
    return val;
    }
    }
    return true;
    }

It only says that I have duplicate when I already add it to the ArrayList and when I get the message I can't add anything else.

input example:

test

test


and then it stops accepting new Strings and only displays the message DUPLICATE! do not add

答案1

得分: 2

正如我在评论中所说:

> 发生这种情况是因为每次调用checkDuplicate时,你实际上都在创建一个ArrayListSet视图,而不是将你要添加的项目与现有列表进行比较。换句话说,你的checkDuplicate方法只在列表中已经存在重复项时才返回true。你需要将新项目与列表进行比较。仔细逐步检查你的逻辑,可以使用调试器,或者手动写下变量的值,你就会看到问题所在。

你可以将这行代码简单地更改为:

if(!names.contains(txtname.getText()))

你实际上并不需要checkDuplicate,因为这有点像是在“重新发明轮子”。ArrayList已经提供了一个可以利用的contains方法。

英文:

As I said in my comment:

> This happens because you are basically creating a Set view of your ArrayList every time you call checkDuplicate rather than comparing the item you're trying to add with the existing list. In other words your checkDuplicate is written such that it only returns true when a duplicate already exists within the list. You need to compare the new item with the list instead. Step through your logic carefully either with a debugger or by manually writing down the values of your variables and you will see the problem.

You could simply change this line:

if(checkDuplicate(names)==true)

to this:

if(!names.contains(txtname.getText()))

You don't really need checkDuplicate since it's "reinventing the wheel" so to speak. ArrayList already provides a contains method that you can use to your advantage.

huangapple
  • 本文由 发表于 2020年5月5日 08:41:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/61603953.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定