简体   繁体   English

我看不到这两行代码的意义

[英]I can't see the point of this two lines of code

public class BruteForceSearch {


    private char[] Text;
    private char[] MyWord;
    private int TextLength;
    private int MyWordLength;


    //word or -1 if not found 
    public int search(String Text, String MyWord) {

        //chars
        this.Text = Text.toCharArray();
        this.MyWord = MyWord.toCharArray();
        this.TextLength = Text.length();
        this.MyWordLength = MyWord.length();


        for (int TextCounter = 0; TextCounter < TextLength - MyWordLength; TextCounter++) {


            int WordCounter = 0;

           //matched increament WordCounter
            while (WordCounter < MyWordLength && this.Text[TextCounter + WordCounter] == this.MyWord[WordCounter]) {
                WordCounter++;
            }

            if (WordCounter == MyWordLength) {
                return TextCounter;
            }

        }
        // return -1 in case you didn't find the word
        return -1;
    }   

here comes my question what is the point of these loops why the start and end like that the for loop (TextCounter < TextLength - MyWordLengt) the while loop ( while (WordCounter < MyWordLength && this.Text[TextCounter + WordCounter] == this.MyWord[WordCounter] ) 这是我的问题,这些循环的意义何在,为什么开始和结束都像for循环(TextCounter < TextLength - MyWordLengt)而while循环( while (WordCounter < MyWordLength && this.Text[TextCounter + WordCounter] == this.MyWord[WordCounter]

The point is: Textcounter starts looking at the the array containing the whole text, and for each characater of the text, which are stored in the array Text[], looks for the next letters, and checks if they match the same sequence of letters of the array MyWord[], which contains the word, and if every char match, then it returns the position in the text where the matching word is 关键是:Textcounter开始查看包含整个文本的数组,并针对存储在Text []数组中的每个字符,查找下一个字母,并检查它们是否与相同的字母序列匹配包含单词的数组MyWord []的数组,如果每个字符都匹配,则它将返回文本中匹配单词所在的位置

The only problem is that the world i'm looking for is for example (champ), if in the text there is a word (championship) it will say that they match, but it should say they don't 唯一的问题是我正在寻找的世界是(冠军),如果文本中有一个单词(冠军),它将说他们匹配,但应该说他们不匹配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM