问题描述
限时送ChatGPT账号..我正在尝试用 Java 读取某个文件并将其转换为多维数组.每当我从脚本中读取一行代码时,控制台都会说:
Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
我知道这个错误是由于编码无法到达特定索引导致的,但我目前不知道如何解决.
这是我的编码示例.
int x = 1;而 (scanner.hasNextLine()) {字符串行 = 扫描仪.nextLine();//分解字符串行String[] Guild = line.split("\\|");//将该值添加到公会数组for (int i = 0; i < Guild.length; i++) {((ArrayList)guildsArray.get(x)).add(Guild[i]);if(sender.getName().equals(Guild[1])) {//此人是Guild[0]的所有者ownerOfGuild = 公会[0];}}x++;}
**文本文档**
测试|baseman101|baseman101|0|测试 2|玩家 2|玩家 2|0|
其他解决方案,例如此处找到的解决方案:写入文本在Java中没有覆盖的文件
提前致谢.
解决方案problem 1 -> int x = 1;
解决方案:x 应该从 0 开始
问题 2->
((ArrayList)guildsArray.get(x)).add(Guild[i]);
你正在增加 x
所以 如果 x >= guildsArray.size()
那么你会得到 java.lang.IndexOutOfBoundsException
>
解决办法
if( x >= guildsArray.size())guildsArray.add(new ArrayList());for (int i = 0; i < Guild.length; i++) {((ArrayList)guildsArray.get(x)).add(Guild[i]);if(sender.getName().equals(Guild[1])) {//此人是Guild[0]的所有者ownerOfGuild = 公会[0];}}
I'm attempting to read a certain file in Java and make it into a multidimensional array. Whenever I read a line of code from the script, The console says:
Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
I know that this error is caused when the coding can't reach the specific index, but I have no idea how to fix it at the moment.
Here is an example of my coding.
int x = 1;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
//Explode string line
String[] Guild = line.split("\\|");
//Add that value to the guilds array
for (int i = 0; i < Guild.length; i++) {
((ArrayList)guildsArray.get(x)).add(Guild[i]);
if(sender.getName().equals(Guild[1])) {
//The person is the owner of Guild[0]
ownerOfGuild = Guild[0];
}
}
x++;
}
**Text Document **
Test|baseman101|baseman101|0|
Test2|Player2|Player2|0|
Other solutions, such as the one found here: Write to text file without overwriting in Java
Thanks in advance.
解决方案problem 1 -> int x = 1;
solution: The x should be start with 0
problem 2->
((ArrayList)guildsArray.get(x)).add(Guild[i]);
You are increasing x
so if x >= guildsArray.size()
then you will get java.lang.IndexOutOfBoundsException
solution
if( x >= guildsArray.size())
guildsArray.add(new ArrayList());
for (int i = 0; i < Guild.length; i++) {
((ArrayList)guildsArray.get(x)).add(Guild[i]);
if(sender.getName().equals(Guild[1])) {
//The person is the owner of Guild[0]
ownerOfGuild = Guild[0];
}
}
这篇关于Java ArrayList IndexOutOfBoundsException 索引:1,大小:1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论