从阵列中删除重复的字符串? [英] Removing duplicate strings from an array?

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

问题描述

我怎样才能把一个字符串数组重复的字符串,而无需使用HashSet的?

我尝试使用循环,但话不能删除。

 的StringBuffer outString =新的StringBuffer(我们的,AIM和,是不是,容易,你,你是,实际的和,是,不是,并提高,实现,而且,很明显,而且,照明,是);单词表= outString.toString()分裂();
对于(i = 0; I< wordList.length;我++){
  为(J = 0; J< wordList.length; J ++){
    如果((单词表[i] =单词表[J])及!及(J> I)){
      T =真实的;
    }
  }
  如果(T ==真){
    ķ++;
  }
}
的String [] = wordList1新的String [K];
单词表= outString.toString()分裂();
对于(i = 0; I< wordList.length;我++){
  (J = 0; J< wordList.length; J ++){
    如果((单词表[i] =单词表[J])及!及(J> I)){
      T =真实的;
    }
  }
  如果(T ==真){
    wordList1 [I] =单词表[I]
  }
}


解决方案

试试这个code删除DUP的话:

  StringBuilder的SB =新的StringBuilder();
的for(int i = 0; I< wordList.length;我++){
    布尔发现= FALSE;
    对于(INT J = I + 1; J< wordList.length; J ++){
        如果(单词表[J] .equals(单词表[I])){
            发现= TRUE;
            打破;
        }
    }
    // System.out.printf(检查:[%S]%N,词表[I]);
    如果(!找到){
        如果(sb.length()大于0)
            sb.append('');
        sb.append(单词表[I]);
    }
}
System.out.printf(独特之处:[%S]%N,某人);

How can I remove duplicate strings from a string array without using a HashSet?

I try to use loops, but the words not delete.

StringBuffer outString = new StringBuffer("Our, aim, and, isn't, easy, you, you're, actual, and, are, aren't, and, improve, achieving, and, Obviously, and, illumination, are");

wordList = outString.toString().split(", ");
for (i = 0; i < wordList.length; i++) {
  for (j = 0; j < wordList.length; j++) {
    if((wordList[i]!=wordList[j])&&(j>i)){
      t=true;
    }
  }
  if(t==true){
    k++;
  }
}
String[] wordList1 = new String[k];
wordList = outString.toString().split(", ");
for (i = 0; i < wordList.length; i++) {
  (j = 0; j < wordList.length; j++) {
    if((wordList[i]!=wordList[j])&&(j>i)){
      t=true;
    }
  }
  if(t==true){
    wordList1[i]=wordList[i];
  }
}

解决方案

Try this code to remove dup words:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < wordList.length; i++) {
    boolean found = false;
    for (int j = i+1; j < wordList.length; j++) {
        if (wordList[j].equals(wordList[i])) {
            found = true;
            break;
        }
    }
    // System.out.printf("Checking: [%s]%n", wordList[i]);
    if (!found) {
        if (sb.length() > 0)
            sb.append(' ');
        sb.append(wordList[i]);
    }
}
System.out.printf("Unique: [%s]%n", sb);

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

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