Java ArrayList IndexOutOfBoundsException索引:1,大小:1

bas*_*101 4 java arrays file arraylist bukkit

我正在尝试用Java读取某个文件并将其转换为多维数组.每当我从脚本中读取一行代码时,控制台会说:

Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
Run Code Online (Sandbox Code Playgroud)

我知道这个错误是在编码无法达到特定索引时引起的,但我现在还不知道如何修复它.

这是我编码的一个例子.

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++;
}
Run Code Online (Sandbox Code Playgroud)

**文字文件**

Test|baseman101|baseman101|0|
Test2|Player2|Player2|0|
Run Code Online (Sandbox Code Playgroud)

其他解决方案,例如在此处找到的解决方案:写入文本文件而不用Java覆盖

提前致谢.

Pra*_*ran 8

问题1 - > int x = 1;
解决方案:x应该从0开始

问题2->

((ArrayList)guildsArray.get(x)).add(Guild[i]);
Run Code Online (Sandbox Code Playgroud)

你正在增加, x 所以if 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])) {
      //The person is the owner of Guild[0]
      ownerOfGuild = Guild[0];
    }
  }
Run Code Online (Sandbox Code Playgroud)