微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

变量自身递增,而不实际更改

如何解决变量自身递增,而不实际更改

| 好吧..所以我正在做一个关于NLP的程序。它使用功能excludeStopWords()。该函数从(检测到的令牌的)二维数组\“ sentTokens \”中读取。在下面的代码中,索引i是句子编号,j是第i个句子中的每个标记。 现在,我的excludeStopWords()所做的是这样的: 它从文本文件中读取停用词并将其存储在TreeSet中 从sendTokens数组中读取令牌,并检查其是否为停用词。如果它们是并置,则不应检查它们是否有停用词,它们只会被转储到finalTokens数组中。如果它们不是集合,则将分别检查它们是否包含停用词,并且仅当它们不是停用词时才添加到finalTokens数组中。 问题出在步骤2的循环中。这是它的一些代码:(我在错误实际发生的位置标记了// //在这里……快结束了)
private void eliminateStopWords() {

    try {

        // Loading TreeSet for stopwords from the file.
        stopWords = new TreeSet<String> ();
        fin = new File(\"stopwords.txt\");
        fScan = new Scanner(fin);
        while (fScan.hasNextLine()) 
            stopWords.add(fScan.nextLine());

        fScan.close();

        /* Test code to print all read stopwords
        iter2 = stopWords.iterator();
        while (iter2.hasNext())
            System.out.println(iter2.next()); */

        int k=0,m=0;    // additional indices for finalTokens array
        System.out.println(NO_OF_SENTENCES);

 newSentence: for(i=0; i < NO_OF_SENTENCES; i++)
          {

        System.out.println(\"i = \" + i);
            for (j=0; j < sentTokens[i].length; j+=2)
            {

        System.out.println(\"j = \" + j);

                // otherwsise,get two successive tokens
                    String currToken = sentTokens[i][j];
                    String nextToken = sentTokens[i][j+1];
                    System.out.println(\"i = \" + i);
                    System.out.println(currToken + \" \" + nextToken);
                    if ( iscollocation(currToken,nextToken) ) {    
// if the current and next tokens form a bigram collocation,they are not checked for stop words
                        // but are directly dumped into finalTokens array
                        finalTokens[k][m] = currToken; m++;
                        finalTokens[k][m] = nextToken; m++;
                    }

                    if ( !stopWords.contains(currToken) )
                    {   finalTokens[k][m] = currToken; m++;  }

                    if ( !stopWords.contains(nextToken) )
                    {       finalTokens[k][m] = nextToken; m++; }


                // if current token is the last in the sentence,do not check for collocations,only check for stop words
                // this is done to avoid ArrayIndexOutOfBounds Exception in sentences with odd number of tokens

// HERE
                    System.out.println(\"i = \" + i);

                    if ( j==sentTokens[i].length - 2) {
                    String lastToken = sentTokens [i][++j];
                    if (!stopWords.contains(lastToken))
                    {  finalTokens[k][m] = lastToken; m++; }

                    // after analyzing last token,move to analyzing the next sentence

                    continue newSentence;

                    }
            }

            k++;    // next sentence in finalTokens array
        }

        // Test code to print finalTokens array
           for(i=0; i < NO_OF_SENTENCES; i++) {
               for (j=0; j < finalTokens[i].length; j++) 
                   System.out.print( finalTokens[i][j] + \" \" );

               System.out.println();
           }



    }
        catch (Exception e) {
            e.printstacktrace();
        }
}
我已经在它们各自的for循环的入口处打印了索引i和j ...对于循环的第一次迭代,它们都可以正常工作,但是当循环即将结束时...我再次打印了该值\'i \'。这次的结果是14。 它从0开始第一次迭代... 不会在循环中的任何地方被操纵... 并在(仅)第一次迭代结束时,将值打印为14 我的意思是,这是我使用Java时遇到的最严重的WEIRDEST错误。它在最后的if块之前抛出Arrayindexoutofboundsexception。就像魔术一样。您无需对代码中的变量执行任何操作,但值仍会更改。这怎么会发生?     

解决方法

您从未在代码中声明
i
j
,这使我相信它们是字段。 我很确定您的某些其他方法会重新使用这些变量,从而使结果混乱。
isCollocation
看起来像是候选人。
for
循环中的计数器应始终是局部变量,理想情况下应在ѭ4itself语句本身内声明(以确保最小范围)。其他一切都只是在自找麻烦(如您所见)。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。