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

编程入门 行业动态 更新时间:2024-10-25 18:24:32
本文介绍了Java ArrayList IndexOutOfBoundsException 索引:1,大小:1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-16 10:30:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/885028.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   大小   Java   ArrayList   IndexOutOfBoundsException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!