Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 即使使用了集合,也会在数组中重复_Java_Arrays_Set - Fatal编程技术网

Java 即使使用了集合,也会在数组中重复

Java 即使使用了集合,也会在数组中重复,java,arrays,set,Java,Arrays,Set,对于类项目,我们必须获取一个字符串(一个段落),将其放入单个单词的数组中,然后将这些单词放入对象数组的对象中。单词不能重复,所以我使用了一个集合来只获取唯一的值,但只有某些单词在重复!下面是该方法的代码。很抱歉,描述含糊不清 Private void processDocument() { String r = docReader.getLine(); lines++; while(docReader.hasLines()==true) {

对于类项目,我们必须获取一个字符串(一个段落),将其放入单个单词的数组中,然后将这些单词放入对象数组的对象中。单词不能重复,所以我使用了一个集合来只获取唯一的值,但只有某些单词在重复!下面是该方法的代码。很抱歉,描述含糊不清

Private void processDocument() 
    {
    String r = docReader.getLine();
    lines++;
    while(docReader.hasLines()==true)
    {
        r= r+" " +docReader.getLine();
        lines++;
    }
    r = r.trim();
    String[] linewords = r.split(" ");
    while(linewords.length>words.length)
    {
        this.expandWords();
    }
    String[] newWord = new String[linewords.length];
    for(int i=0;i<linewords.length;i++)
    {

        newWord[i] = (this.stripPunctuation(linewords[i]));
    }

    Set<String> set = new HashSet<String>(Arrays.asList(newWord));
    Object[]newArray = set.toArray();
    words = new Word[set.size()-1];
    String newString = null;
    for(int i =0;i<set.size();i++)
    {
        if(i==0)
        {
            newString = newArray[i].toString() + "";
        }
        else
        {
            newString = newString+newArray[i].toString()+" ";
        }
    }
    newString = newString.trim();
    String[] newWord2 = newString.split(" ");
    for(int j=0;j<set.size()-1;j++)
    {


        Word newWordz = new Word(newWord2[j].toLowerCase());
        words[j] = newWordz;

    }
Private void processDocument()
{
字符串r=docReader.getLine();
行++;
while(docReader.hasLines()==true)
{
r=r+“”+docReader.getLine();
行++;
}
r=r.修剪();
字符串[]linewords=r.split(“”);
while(linewords.length>words.length)
{
这是一个单词();
}
String[]newWord=新字符串[linewords.length];

对于(int i=0;i我认为问题在于,当您将其放入HashSet时,单词的大写形式不同,导致HashCode不同。从文件中读取时,请将所有内容转换为小写,这样应该可以正常工作

newWord[i] = (this.stripPunctuation(linewords[i])).toLowerCase();
试试这个:

public String[] unique(String[] array) {
   return new HashSet<String>(Arrays.asList(array)).toArray();
}
公共字符串[]唯一(字符串[]数组){
返回新的HashSet(Arrays.asList(array)).toArray();
}
厚颜无耻地抄袭别人的作品

此外,正如@Brinnis所指出的,请确保单词被修剪并使用正确的大小写

for(int i = 0; i < linewords.length; i++) {
   newWord[i] = this.stripPunctuation(linewords[i]).toLowerCase(); 
}
String[] newArray = unique(newWord);
for(int i=0;i
那里有很多代码-我强烈怀疑你不需要那么多来演示这个问题。如果你能展示一个简短但完整的程序来演示这个问题,那会很有帮助。你能举个例子吗?一两行输入和从这些行中重复的单词会很有帮助。很抱歉复制了你的代码,我写了完全相同的解决方案,呵呵,投了赞成票,你这个了不起的混蛋,成功了