Removing duplicate strings from an array?

前端 未结 9 743
野的像风
野的像风 2021-01-24 06:54

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 = ne         


        
9条回答
  •  清歌不尽
    2021-01-24 07:44

    You probably want to set t back to false after pulling the value you want:

    if(t)
    {
         wordList1[i]=wordList[i];
         t = false;
    }
    

    Also this:

    if((wordList[i]!=wordList[j])&&(j>i))
    

    Will always return true since strings are immutable (unless you compared a string to an exact reference of itself which you disallow with j>i). You need to change it to say this:

    if (!(wordList[i].equals(wordList[j]))&&(j>i))
    

    Using .equals will compared that they contain the same string, not that they point to the exact reference of a string.

    Not sure if that's the only problems or not, a bit unclear from what's given.

提交回复
热议问题