简体   繁体   English

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

[英]Java ArrayList IndexOutOfBoundsException Index: 1, Size: 1

I'm attempting to read a certain file in Java and make it into a multidimensional array. 我正在尝试用Java读取某个文件并将其转换为多维数组。 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 其他解决方案,例如在此处找到的解决方案: 写入文本文件而不用Java覆盖

Thanks in advance. 提前致谢。

problem 1 -> int x = 1; 问题1 - > int x = 1;
solution: The x should be start with 0 解决方案:x应该从0开始

problem 2-> 问题2->

((ArrayList)guildsArray.get(x)).add(Guild[i]);

You are increasing x so if x >= guildsArray.size() then you will get java.lang.IndexOutOfBoundsException 你正在增加x所以if x >= guildsArray.size()那么你将获得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];
    }
  }

The problem is occurring here: 问题出现在这里:

... guildsArray.get(x) ...

but is caused here: 但是在这里引起:

int x = 1;
while (scanner.hasNextLine()) {
   ...

Because Collections and arrays are zero-based (the first element is index 0 ). 因为集合和数组是从零开始的(第一个元素是索引0 )。

Try this: 试试这个:

int x = 0;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 arraylist indexoutofboundsexception索引0大小0 - arraylist indexoutofboundsexception index 0 size 0 java.lang.IndexOutOfBoundsException:无效的索引2,Arraylist中的大小为2? - java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2 in Arraylist? java.lang.IndexOutOfBoundsException:使用ArrayList时索引:0,大小:0 - java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 while working with ArrayList “线程”main“中的异常 java.lang.IndexOutOfBoundsException: Index: 0, Size: 0” with ArrayList? - “Exception in thread ”main“ java.lang.IndexOutOfBoundsException: Index: 0, Size: 0” with ArrayList? Java - IndexOutOfBoundsException:索引:1,大小:0 - Java - IndexOutOfBoundsException: Index: 1, Size: 0 java.lang.IndexOutOfBoundsException: Index: 1, Size: 0 at java.util.ArrayList.get(ArrayList.java:437) - java.lang.IndexOutOfBoundsException: Index: 1, Size: 0 at java.util.ArrayList.get(ArrayList.java:437) java.lang.IndexOutOfBoundsException: Index: 2, Size: 1 at java.util.ArrayList.get(ArrayList.java:437) - java.lang.IndexOutOfBoundsException: Index: 2, Size: 1 at java.util.ArrayList.get(ArrayList.java:437) IndexOutOfBoundsException:索引:0,大小:0 2D Arraylist - IndexOutOfBoundsException: Index: 0, Size: 0 2D Arraylist IndexOutOfBoundsException at Index: 4, Size: 4 将元素添加到 ArrayList 时 - IndexOutOfBoundsException at Index: 4, Size: 4 when adding elements to an ArrayList java.lang.IndexOutOfBoundsException:索引:0,大小:0 在 java.util.ArrayList.get - java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.get
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM