//思想即为在一个大数组中查找一个小数组
public
static int indexOf(byte[] source, byte[] target) { //先把字符串转化为byte[] source,把要查找的字符串转化为byte[]target int sourceCount = source.length; int targetCount = target.length; byte first = target[0]; int max = (sourceCount - targetCount); for (int i = 0; i <= max; i++) { /* Look for first character. */ if (source[i] != first) { //在source中循环查找出现target的首字符的索引 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 = 1; j < end && source[j] == target[k]; j++, k++) // j即为source出现target首字符的位置+1;k即为target数组中首字符+1
                ;  
  
            if (j == end) {  
                /* Found whole string. */  
                return i;  
            }  
        }  
    }  
    return -1;  
}  

 

posted on 2017-11-23 17:01  艾缇小王子  阅读(335)  评论(0编辑  收藏  举报