for循环实现LastIndexOf 方法
这是我写的实现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是我定义的最后一次出现的位置,可是循环执行到倒数第二次就完了,问题出在哪了呢?
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是我定义的最后一次出现的位置,可是循环执行到倒数第二次就完了,问题出在哪了呢?
...全文
相关推荐
Javascript中indexOf()和lastIndexOf应用方法实例
在实际开发中,可以通过while循环或者for循环来多次查找子字符串的位置。例如,可以设置一个变量i表示当前搜索的起始位置,然后用循环来不断更新这个位置,直到indexOf()返回-1为止。
详解JavaScript中数组和字符串的lastIndexOf()方法使用
### IE8兼容问题IE8及以下版本的Internet Explorer浏览器并不支持`Array.lastIndexOf()`方法。针对这一问题,可以通过自定义一个函数来实现相同的功能。
JS模拟实现ECMAScript5新增的数组方法
== undefined) { index = i; break; } } return index;};```上述代码通过for循环遍历数组,如果找到元素且元素不是undefined,则记录索引并退出循环
javascript循环数组有什么方法_javascript循环数组有哪些方法.docx
### JavaScript 循环数组的方法详解#### 一、基本循环遍历数组**问题描述**:需要简单有效地访问数组中的所有元素。**解决方案**:使用 `for` 循环是最常见的遍历数组的方式。
JS数组对象去重方法[项目代码]
其次,利用双层for循环进行数组去重是另一种常见方法。这种方法通过比较数组中每一个元素与其它所有元素,如果发现有相同的,则从原数组中删除。ES6提供的Map()数据结构也可用于实现数组去重。


Java SE
