簡體   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;
    }   

這是我的問題,這些循環的意義何在,為什么開始和結束都像for循環(TextCounter < TextLength - MyWordLengt)而while循環( while (WordCounter < MyWordLength && this.Text[TextCounter + WordCounter] == this.MyWord[WordCounter]

關鍵是:Textcounter開始查看包含整個文本的數組,並針對存儲在Text []數組中的每個字符,查找下一個字母,並檢查它們是否與相同的字母序列匹配包含單詞的數組MyWord []的數組,如果每個字符都匹配,則它將返回文本中匹配單詞所在的位置

唯一的問題是我正在尋找的世界是(冠軍),如果文本中有一個單詞(冠軍),它將說他們匹配,但應該說他們不匹配

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM