【发布时间】:2013-03-27 13:37:59
【问题描述】:
我创建了一个大小为 45 的数组列表。由于某种原因,我收到了 ArrayOutOfBoundException。
这是我得到错误的地方:
String temp;
Scanner sc = new Scanner(str1);
while(sc.hasNext())
{
temp = sc.next();
int len = temp.length();
if(len > 0)
wordsByLen[len - 1].add(temp); //throwing the error here with exception of 59
}
提前感谢您的帮助!
【问题讨论】:
-
wordsByLen 定义在哪里
-
什么可以保证
temp.length()永远不会高于 45?您正在阅读我能看到的字符串 -
我在类的构造函数中定义了wordsByLen。
-
我可以看到您假设 temp.length() 将返回 0 到 44 之间的值。这是真的吗?这可能会导致问题。
-
尝试在
if条件之前打印len的值,看看你得到了什么值
标签: java arrays arraylist indexoutofboundsexception