我对java很陌生。我想知道是否可以在字符串或字符串数组中的索引中检查一定数量的连续重复字符(“特定数字”由用户确定)。到目前为止我已经尝试过
int multiple_characters = 0;
String array1 [] = {"abc","aabc","xyyyxy"};
for (int index = 0; index < array1.length;i++){
for (int i = 0;i<array1[index].length;i++){
if (array1[index].charAt(i) == array1[index].charAt(i+1)){
multiple_characters++;
}
}
}
但是有了这个我得到一个StringIndexOutOfBounds
错误。我尝试通过添加一个额外的 if 语句来确保 i 不等于 来解决此array1[index].length
问题,但这仍然引发了相同的错误。除了手动和逃避方法之外:
if ((array1[index].charAt(i) == array1[index].charAt(i+1) && (array1[index].charAt(i) == array1[index].charAt(i+2))
并且重复了很多次(这对于快速更改我的代码并不是很好),我似乎找不到解决方案。