输入:输任意入字符串数组S[ ],即主串;
输入任意字符串数组T[ ],即子串;
输出:返回字符串数组T[ ]在字符串数组S[ ]中的的相对位置(只需输出 T [ ]的首字母位置);
(由于输入的字符主串S[ ] 、子串T[ ]是任意的,所以会有多种运行结果)
请各位高手解答:
输入任意字符串数组T[ ],即子串;
输出:返回字符串数组T[ ]在字符串数组S[ ]中的的相对位置(只需输出 T [ ]的首字母位置);
(由于输入的字符主串S[ ] 、子串T[ ]是任意的,所以会有多种运行结果)
请各位高手解答:
怎么感觉你要的是字符串而 不是字符串数组字符串的话直接indexof就ok了
String[] s = {"T","e","s","t"};
String[] t = {"e","s"};
String[] t = {"hello","china"};
这个意思把,求hello china在S[]中出现的位置可否写个代码看看呢
{
int nIndex = -1;
if(T!=null && S!=null && T.length!=0 && S.length !=0){
int nowIndex = -1;
for(int n =0;n<S.length;n++)
{
if (S[n].equals(T[nowIndex+1]))
nowIndex += 1;
if(nowIndex == T.length-1)
{
nIndex = n - nowIndex;
break;
}
}
}
return nIndex;
}
{
String[] s1 = {"Test","hello","china","t"};
String[] t1 = {"hello","china"};
System.out.println(indexOf(s1,t1));
//得到显示1String[] s = {"Test","hello","china","t"};
String[] t = {""};
System.out.println(indexOf(s,t));
//得到显示-1
}