1

我对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))

并且重复了很多次(这对于快速更改我的代码并不是很好),我似乎找不到解决方案。

4

3 回答 3

2

对于内部 for 循环(带有i变量的循环),然后调用string.charAt(i+1)where ii 从 0 循环到该字符串的长度。

难怪你得到一个索引数组越界异常,你在最后一个之后要求字符。

我建议你尝试理解异常,如果你不能,调试你的代码(单步调试,一次一行,如果你不知道如何使用调试器,添加 println 语句,检查什么代码按照您的想法执行。您的代码在哪里与您的预期不同?这就是错误所在)。

这个“哦,它行不通,我会完全放弃它并找到另一种方法”的计划是次优的:) – 回到第一个片段,然后解决这个问题。

于 2020-01-25T23:32:37.890 回答
1

你得到StringIndexOutOfBoundsException是因为你试图访问string.charAt(i + 1)wherei上升到最高索引(即string.length() - 1.string

你可以这样做:

class Main {
    public static void main(String[] args) {
        int multiple_characters = 0;
        int i;
        String array1[] = { "abc", "aabc", "xyyyxy" };
        for (int index = 0; index < array1.length; index++) {
            System.out.println("String: " + array1[index]);
            for (i = 0; i < array1[index].length() - 1; i++) {
                multiple_characters = 1;
                while (array1[index].charAt(i) == array1[index].charAt(i + 1) && i < array1[index].length() - 1) {
                    multiple_characters++;
                    i++;
                }
                System.out.println(array1[index].charAt(i) + " has been repeated consecutively " + multiple_characters
                        + " time(s)");
            }
            if (multiple_characters == 1) {
                System.out.println(array1[index].charAt(i) + " has been repeated consecutively 1 time(s)");
            }
            System.out.println("------------");
        }
    }
}

输出:

String: abc
a has been repeated consecutively 1 time(s)
b has been repeated consecutively 1 time(s)
c has been repeated consecutively 1 time(s)
------------
String: aabc
a has been repeated consecutively 2 time(s)
b has been repeated consecutively 1 time(s)
c has been repeated consecutively 1 time(s)
------------
String: xyyyxy
x has been repeated consecutively 1 time(s)
y has been repeated consecutively 3 time(s)
x has been repeated consecutively 1 time(s)
y has been repeated consecutively 1 time(s)
------------
于 2020-01-25T23:54:35.290 回答
1

如果我要寻找重复的字符,我会走正则表达式路线。例如,要查找重复的a字符(在本例中重复两次),您可以:

import java.util.regex.Pattern;

public class Temp {
  public static void main(final String[] args) {
    String array1 [] = {"abc","aabc","xyyyxy"};
    for (String item : array1){
      if (Pattern.compile("[a]{2}").matcher(item).find()) {
        System.out.println(item + " matches");
      }
    }
  }
}

在这个摘录中,reg exp"[a]{2}"用于查找重复两次的任何字符序列。当然,更复杂的匹配需要更复杂的正则表达式,可以在这里找到解释这一点的好资源: https ://docs.oracle.com/en/java/javase/11/docs/api/java.base/java /util/regex/Pattern.html
另一点是,为了提高效率,通常将:移到 Pattern.compile(*Pattern*) 方法调用之外,例如移到final static field

此堆栈溢出:
RegEx 不超过 2 个相同的连续字符和 aZ 和 0-9
对涉及此问题的正则表达式问题进行了相当详细的描述。

于 2020-01-26T01:24:23.210 回答