爪哇 - 删除重复的一个ArrayList [英] Java - Removing duplicates in an ArrayList

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

问题描述

我正在使用的的ArrayList 存储字符串的程序。程序会提示用户提供一个菜单,并允许用户选择要执行的操作。这样的操作添加到字符串列表,打印项等。我想要的是能够做的就是创建一个名为方法 removeDuplicates()。该方法将搜索的ArrayList ,并删除任何重复的值。我想离开这个列表中的重复值(S)的一个实例。我还希望此方法返回除去重复项的总数。

我一直在尝试使用嵌套的循环来实现这一点,但我一直在运行陷入困境,因为当条目被删除时,的ArrayList 的索引被改变,事情并不像他们应该工作。我知道概念是什么,我需要做的,但我无法实现该想法在code。

下面是一些伪code:

开始第一个条目; 检查列表中的每个后续项,看看它的第一个条目相匹配; 除去,所述第一条目相匹配的列表中的每个随后的登记项;

所有条目都被检查

后,转移到第二个条目; 检查列表中的每个条目,看看它的第二项匹配; 除去,所述第二条目相匹配的列表中的每个条目;

在列表中重复入境

这里的code我到目前为止有:

 公众诠释removeDuplicates()
{
  诠释重复= 0;

  的for(int i = 0; I< strings.size();我++)
  {
     对于(INT J = 0; J< strings.size(); J ++)
     {
        如果(我== j)条
        {
          // I和Ĵ指同一个条目,以便做什么
        }

        否则,如果(strings.get(J).equals(strings.get(I)))
        {
           strings.remove(J);
           重复++;
        }
     }
 }

   返回副本;
}
 

更新:看来,威尔正在寻找,涉及开发算法来删除重复,而不是使用设置一个务实的解决方案,一门功课的解决方案。见他的评论:

Thx的建议。这是一个任务的一部分,我相信老师曾打算为解决不包括套。换句话说,我要拿出这将搜索并删除重复没有实施的HashSet 的解决方案。使用嵌套循环这就是我想要做的,但我一直有一些问题的ArrayList 某些条目被删除后,

解决方案

为什么不使用集合,如设置(和像一个实施的HashSet ),这自然prevents重复?

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.

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.

Here is some pseudo 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;

repeat for entry in the list

Here's the code I have so far:

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;
}

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:

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.

解决方案

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

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

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