一切正常。-6ren">
gpt4 book ai didi

java - 我从匹配器点查找中得到indexoutofbound异常

转载 作者:行者123 更新时间:2023-11-30 05:23:01 27 4
gpt4 key购买 nike

我面临一个错误,其中我的代码仅在我的两个正则表达式(我的正则表达式正在寻找 Text1)未彼此相邻编写时才有效。例如,我有一个 text ="Text1, Text2, Text3" -> 一切正常。如果我有这样的 ="Text1, Text1, Text2, Text3" -> 我的代码会抛出 IndexOutOfBoundsException

我认为它与 matcher.find() 有关,但我无法弄清楚它到底是如何工作的。

int i = 0;
while(matcher.find()) {
if(!array.contains(matcher.group())){
try {
array.add(i, matcher.group());
array.set(i, array.get(i).replaceAll("\\.",""));
array.set(i, array.get(i).replaceAll("\\W","-"));

}
catch (Exception e) {
e.printStackTrace();
}
}
i++;
}
result="";

最佳答案

即使您没有向数组添加任何内容,您也会增加计数器 (i)。下次您尝试访问当前项目时,您的索引太大,导致 IndexOutOfBoundException

关于java - 我从匹配器点查找中得到indexoutofbound异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59211355/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com