java - 我从匹配器点找到 indexoutofbound 异常
问题描述
我面临一个错误,我的代码只有在我的两个 RegEx(我的 RegEx 正在寻找 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
.
推荐阅读
- bots - Telegram HTML5 Game Bot 返回空白页面
- android - 为没有 Cordova 插件的 Android PWA 发送本地推送通知
- sql-server - PowerShell 将大于 2 GB 的文件插入 SQL FileStream-Database
- python - 查找字典列表的总和
- reactjs - 如何在下一个 js 中停止不可见的控制台登录?
- reporting-services - SSRS 不接受固定值
- .net - 错误 MSB4041。使用 Visual Studio Enterprise 2019 项目的默认 XML 命名空间必须是 MSBuild XML 命名空间
- python - 带有 xbbg BDP 调用的间歇性空白 DataFrame
- python - 使用 Python 从 Outlook 邮件正文中的 CSV 文件发送表格内容
- html - 如何在 CSS 中准确地将导航栏居中?