删除字符串数组重复的字符串 [英] Delete duplicate strings in string array

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

问题描述

我正在用Java基于字符串处理的程序中,我需要从一个字符串数组删除重复的字符串。在这个程序中,所有的字符串大小相同。

在'阵',这是一个字符串数组包含许多在两个字符串彼此相似的字符串。因此,使用低于code中的重复的字符串必须得到删除,但它不会被删除。

如何删除重复的字符串?

我用下面的code。

 为(INT S = 0; S< array.length-1; ​​S ++)
    {
        对于(INT M = 0; M< array.length; M +)
        {
                对于(INT N = 0; N<阵[M]。长度(); N ++)
                {
                    如果(阵列[S] .charAt(N)==阵列[M]。.charAt(N))
                    {
                      继续;
                    }
                    其他
                打破;
        }
        如果(N ==阵列[M]。长度())
        {
            ArrayUtils.removeElement(数组,数组[S]);
        }
    }


解决方案

这将工作

  =数组新的HashSet<串GT;(Arrays.asList(阵列))的toArray(新的String [0])。

或只使用了 HashSet的不是数组。

I am making a program based on string processing in Java in which I need to remove duplicate strings from a string array. In this program, the size of all strings are same.

The 'array' which is a string array contains a number of strings in which two strings resemble each other. So using the below code the duplicate string must get removed but it is not removed.

How to remove the duplicate strings?

I am using the following code.

    for(int s=0;s<array.length-1;s++)
    {
        for(int m=0;m<array.length;m++)
        {
                for(int n=0;n<array[m].length();n++)
                {   
                    if(array[s].charAt(n)==array[m].charAt(n))
                    {
                      continue;
                    }
                    else 
                break;
        } 
        if(n==array[m].length())
        {
            ArrayUtils.removeElement(array, array[s]);
        }
    }

解决方案

This will work

array = new HashSet<String>(Arrays.asList(array)).toArray(new String[0]);

or just use a HashSet instead of an array.

这篇关于删除字符串数组重复的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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