删除字符串数组中的重复字符串

23

我正在用Java进行基于字符串处理的程序设计,需要从字符串数组中删除重复的字符串。在此程序中,所有字符串的大小都相同。

'array'是一个字符串数组,其中有许多相似的字符串。使用以下代码应该可以去除重复的字符串,但实际上却没有去除。

如何去除重复的字符串?

我正在使用下面的代码:

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

你现在的代码有什么问题? - Andy
3
为什么不使用更适合去重的结构,比如 HashSet,而是使用数组呢? - assylias
15个回答

0
public static List<String> sortHandleArrayList(String... arrayInput) {
    List<String> list = new ArrayList<>();
    for (String string : arrayInput) {
        if (!list.contains(string)) {
            list.add(string);
        }
    }
    Collections.sort(list);
    return list;
}

0

在不使用集合的情况下从String []中删除重复的字符串

    public static void removeDuplicate(String[] str, int size){
            for(int i=0; i<size-1; i++){
                if(str[i]!=null){
                    for(int j=i+1; j<size-1; j++){
                        if(str[i].equals(str[j])){
                            str[j]=null;
                        }
                    }
                }
            }
            for(int i=0; i<size;i++){
                if(str[i]==null)
                   continue;
                System.out.println(str[i]);
            }
        }

0
     String[] arr = {"w10","w20","w10","w30","w20","w40","w50","w50"};
     List<String> arrList = new ArrayList<String>();
     int cnt= 0;
       //List<String> arrList = Arrays.asList(arr);
       List<String> lenList = new ArrayList<String>();
          for(int i=0;i<arr.length;i++){
        for(int j=i+1;j<arr.length;j++){
           if(arr[i].equals(arr[j])){
             cnt+=1;
           }                
        }
        if(cnt<1){
          arrList.add(arr[i]);
        }
          cnt=0;
        }

for(int k=0;k<arrList.size();k++){
            System.out.println("Array without Duplicates: "+arrList.get(k));
        }

0
List<String> al = new ArrayList<String>();
String[] months={"Jan","Feb","Mar","Apr","Jan","Mar","May","May"};
for(int i=0;i<months.length;i++){
    for(int j=1;j<months.length;j++){
        if(months[i].equalsIgnoreCase(months[j])){
            if(!al.contains(months[i])){
                al.add(months[i]);
            }
        }
    }
}

-3

去重整数:这是完美的答案 /// 哈里斯 ///

public static void duplicateRemove(int[] arr) {
    int temp = 0;

    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr.length; j++) {
            if (arr[i] < arr[j]) {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }

    int count;
    for (int j = 0; j < arr.length;) {
        count = 1;
        for (int i = j + 1; i < arr.length; i++) {
            if (arr[i] == arr[j]) {
                count++;
            } else
                break;

        }
        System.out.println(arr[j] + " is :  " + count);
        j += count;
    }

}

1
问题涉及删除重复的字符串而不是整数。 - ılǝ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接