Java - 删除 ArrayList 中的重复项 [英] Java - Removing duplicates in an ArrayList

查看:26
本文介绍了Java - 删除 ArrayList 中的重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用 ArrayList 来存储 Strings 的程序.该程序通过菜单提示用户并允许用户选择要执行的操作.此类操作是将字符串添加到列表、打印条目等.我想要做的是创建一个名为 removeDuplicates() 的方法.此方法将搜索 ArrayList 并删除任何重复的值.我想在列表中留下一个重复值的实例.我还希望此方法返回删除的重复项总数.

I'm working on a program that uses an ArrayList to store Strings. The program prompts the user with a menu and allows the user to choose an operation to perform. Such operations are adding Strings to the List, printing the entries etc. What I want to be able to do is create a method called removeDuplicates(). This method will search the ArrayList and remove any duplicated values. I want to leave one instance of the duplicated value(s) within the list. I also want this method to return the total number of duplicates removed.

我一直在尝试使用嵌套循环来完成此操作,但我遇到了麻烦,因为当条目被删除时,ArrayList 的索引会被更改,并且事情无法正常工作他们应该.我从概念上知道我需要做什么,但我在代码中无法实现这个想法.

I've been trying to use nested loops to accomplish this but I've been running into trouble because when entries get deleted, the indexing of the ArrayList gets altered and things don't work as they should. I know conceptually what I need to do but I'm having trouble implementing this idea in code.

这是一些伪代码:

从第一个条目开始;检查列表中的每个后续条目,看看它是否与第一个条目匹配;删除列表中与第一个条目匹配的每个后续条目;

start with first entry; check each subsequent entry in the list and see if it matches the first entry; remove each subsequent entry in the list that matches the first entry;

检查完所有条目后,转到第二个条目;检查列表中的每个条目,看看它是否与第二个条目匹配;删除列表中与第二个条目匹配的每个条目;

after all entries have been examined, move on to the second entry; check each entry in the list and see if it matches the second entry; remove each entry in the list that matches the second entry;

重复进入列表

这是我目前的代码:

public int removeDuplicates()
{
  int duplicates = 0;

  for ( int i = 0; i < strings.size(); i++ )
  {
     for ( int j = 0; j < strings.size(); j++ )
     {
        if ( i == j )
        {
          // i & j refer to same entry so do nothing
        }

        else if ( strings.get( j ).equals( strings.get( i ) ) )
        {
           strings.remove( j );
           duplicates++;
        }
     }
 }

   return duplicates;
}

更新:似乎 Will 正在寻找一种家庭作业解决方案,该解决方案涉及开发算法以删除重复项,而不是使用 Sets 的务实解决方案.看他的评论:

UPDATE: It appears that Will is looking for a homework solution that involves developing the algorithm to remove duplicates, rather than a pragmatic solution using Sets. See his comment:

感谢您的建议.这是作业的一部分,我相信老师打算让解决方案不包括集合.换句话说,我将提出一个解决方案,该解决方案将在不实现 HashSet 的情况下搜索和删除重复项.老师建议使用嵌套循环,这正是我正在尝试做的,但在删除某些条目后,我在 ArrayList 的索引方面遇到了一些问题.

Thx for the suggestions. This is part of an assignment and I believe the teacher had intended for the solution to not include sets. In other words, I am to come up with a solution that will search for and remove duplicates without implementing a HashSet. The teacher suggested using nested loops which is what I'm trying to do but I've been having some problems with the indexing of the ArrayList after certain entries are removed.

推荐答案

为什么不使用像 Set 这样的集合(以及像 HashSet 这样的实现)自然防止重复?

Why not use a collection such as Set (and an implementation like HashSet) which naturally prevents duplicates?

这篇关于Java - 删除 ArrayList 中的重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆