for循环实现LastIndexOf 方法

aaaa8215 aaaa8215 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
这是我写的实现LastIndexOf 方法的功能
public class LastIndexOf
{
public static void main(String[] args)
{
String srcText = "cx8923ulfk;dji-abc321fckdu9a-832abc-894ofjdksau9-83791piufdsiaabcfodsa";
String str = "abc";
int hits = 0;
int idx = 0 ;
for( int i = 0; i < srcText.length(); i ++)
{
if(srcText.charAt(i) == str.charAt(hits))
{
hits += 1;
if( hits == str.length())
{
idx = i - str.length() + 1;
hits = 0;
}
}
else
hits = 0;

}
System.out.println(idx);
System.out.println(srcText.lastIndexOf("abc"));
}
} idx是我定义的最后一次出现的位置,可是循环执行到倒数第二次就完了,问题出在哪了呢?
...全文

相关推荐

评论 3
收藏 已收藏
分享
返回顶部
aaaa8215
Java SE 62,620 成员 307,251 内容