2017-11-17 8 views
0

私はString.classのソースコードを読んでいます。
方法ではindexOf()私は理解できない何かを見ます。
indexOf()ソースコードからString.classのコードスニペットを取得します。なぜコードはString.classのindexOf()メソッドをこのように記述しますか?

/** 
* Code shared by String and StringBuffer to do searches. The 
* source is the character array being searched, and the target 
* is the string being searched for. 
* 
* @param source  the characters being searched. 
* @param sourceOffset offset of the source string. 
* @param sourceCount count of the source string. 
* @param target  the characters being searched for. 
* @param targetOffset offset of the target string. 
* @param targetCount count of the target string. 
* @param fromIndex the index to begin searching from. 
*/ 
static int indexOf(char[] source, int sourceOffset, int sourceCount, 
     char[] target, int targetOffset, int targetCount, 
     int fromIndex) { 
    if (fromIndex >= sourceCount) { 
     return (targetCount == 0 ? sourceCount : -1); 
    } 
    if (fromIndex < 0) { 
     fromIndex = 0; 
    } 
    if (targetCount == 0) { 
     return fromIndex; 
    } 

    char first = target[targetOffset]; 
    int max = sourceOffset + (sourceCount - targetCount); 

    for (int i = sourceOffset + fromIndex; i <= max; i++) { 
     /* Look for first character. */ 
     if (source[i] != first) { 
      while (++i <= max && source[i] != first); 
     } 

     /* Found first character, now look at the rest of v2 */ 
     if (i <= max) { 
      int j = i + 1; 
      int end = j + targetCount - 1; 
      for (int k = targetOffset + 1; j < end && source[j] 
        == target[k]; j++, k++); 

      if (j == end) { 
       /* Found whole string. */ 
       return i - sourceOffset; 
      } 
     } 
    } 
    return -1; 
} 

私はここでコードを理解できません。

if (fromIndex >= sourceCount) { 
    return (targetCount == 0 ? sourceCount : -1); 
} 

source Stringは、sourceOffset2で、"abcdedefg"あるsourceCount3で、 、私はこのことから "d" を検索したい場合は、 は、なぜ私は、インデックス4から検索できないのですか?
/**
* Ps:sourceCountが文字列全体の長さを意味する場合は、source.length
*を代わりに使用してください。
*/

+1

*写真*のコード、投稿**コード**は投稿しないでください。理由:http://meta.stackoverflow.com/q/285551/157247 –

+0

テキスト自体ではなく、コードやエラーの画像を投稿したので、私はこの質問をd​​ownvotedしました。ボランティアや将来の来場者を含め、誰もが使用することが困難な画像です。イメージを削除して関連するテキストに置き換えた場合、私はダウンボートを引っ込めることを検討します。参照:[質問するときにSOのコードをSOにアップロードしないとなぜですか?](https://meta.stackoverflow.com/questions/285551/why-not-to-upload-images-of-code-on-so - 質問の際に) –

+1

私はこれを知らないと申し訳ありません。私は変更しています。 – Saber

答えて

2

このメソッドは、2つの文字配列を受け入れます。source配列は検索対象の配列で、target配列は検索対象の配列です。

ただし、offset変数とcount変数は、検索範囲をsourceのサブ配列とtargetのサブ配列に制限します。

基本的にはtarget[targetOffSet]からtarget[targetOffset+targetCount-1]にサブアレイ内の文字で構成Stringためsource[sourceOffSet]からsource[sourceOffset+sourceCount-1]にサブアレイで検索されています。

ここにイラストがあります。

source array : |--------------------| 
sub array :  |-------|   
        source source 
        offset offset + 
          source 
          count - 1 

target array : |--------------------| 
sub array :  |-------|   
        target target 
        offset offset + 
          target 
          count - 1 

しかしながら、検索がさらにfromIndexを供給することによって制限される:検索に関連する配列はサブアレイです。 sourceサブアレイのfromIndex番目のインデックスから検索を開始します。 sourceサブ配列の長さは

targetサブアレイが空でない限りfromIndex >= sourceCount場合、targetサブアレイは、-1が返される(即ちtargetCount == 0)、見つけることができない、sourceCountあります。

のはあなたの例を考えてみましょう:

source : "abcdedefg" 
sourceOffset : 2 
sourceCount : 3 
target : "d" 
targetOffset : 0 
targetCount : 1 
fromIndex : 4 

これらのパラメータは、あなたがインデックス4から始まるターゲットサブ文字列"d"のソース部分文字列"cde"で検索されている意味。ただし、インデックス4"cde"に存在しないため、-1が返されます。

あなた

シモンズ:sourceCountは、文字列全体の長さを意味している場合、私はの長さを意味するものではありませんsourceCountを説明したように、なぜsource.length代わり

を使用しません全体source配列、検索されるサブ配列のちょうど長さ。あなたがsomeString.indexOf(str,fromIndex)を呼び出すときに、あなたがについて尋ねstatic方法は、以下のパラメータで呼び出されていることを

注:

public int indexOf(String str, int fromIndex) { 
    return indexOf(value, 0, value.length, 
      str.value, 0, str.value.length, fromIndex); 
} 

この場合sourceCountsource.lengthに等しい(すなわち全体source配列は、開始を検索されますfromIndex)。

+0

ありがとうございます。したがってfromIndexはオフセット文字から始まりますが、文字列全体のインデックスではなく、この部分文字列のインデックスであり、0 ~~ "cde"は相対インデックス0,1,2を持ちます。そうですか? – Saber

+0

@Saber正しいです。また、 'int i = sourceOffset + fromIndex'で始まるforループから見ることができます。 – Eran

0
if (fromIndex >= sourceCount) { 
     return (targetCount == 0 ? sourceCount : -1); 
    } 

これは文字列の長さ(sourceCount)、検索インデックス(fromIndex)で終了した後であると言う、その後、求められた文字列が""でない限り(見つからない)-1を返す - 常に」であります見つかりました。かなり具体的です。

+0

sourceCountは文字列の最後を意味しますか?私は部分文字列の終わりだと思った。 – Saber

+0

実際、上記のjavadocは少し曖昧です: '検索する文字をソースします'は両方を意味することができますが、 '検索対象の文字をターゲットにします.'はソースが大きな文字列であることを明らかにし、彼らは「求められた鍵」などを使用していたはずです。 –

関連する問題