【问题标题】:Index out of bounds, but I have no idea where [duplicate]索引超出范围,但我不知道在哪里[重复]
【发布时间】:2020-09-14 17:58:25
【问题描述】:

这些是我的代码的 sn-ps,但我一直收到同样的错误,我不知道为什么。

我不断得到

线程“main”java.lang.ArrayIndexOutOfBoundsException 中的异常:索引 2 超出长度 2 的范围

在语法上。(Grammar.java:92)

我可以看出它可能与数组大小有关,但我不知道在哪里。

提前谢谢你,如果有人可以帮助我,将不胜感激。

public int getInit() {
        return init;
    }

int max = 100;
    int[][] ntr = new int[max][0];

NTR tempo = nttemp.remove(0); // NTR is another class that contains only gets
            int tempoF = tempo.getFirst();
            int tempoS = tempo.getSecond();


int i = 1;
92 ntr[tempo.getInit() - 'A'][i] = tempoF;
i++;
ntr[tempo.getInit() - 'A'][i] = tempoS;
i++;

【问题讨论】:

  • edit您的问题并发布您获得的整个堆栈跟踪。还要在文件Grammar.java 中发布第 92 行的文本。

标签: java arrays indexoutofboundsexception


【解决方案1】:

您创建了一个大小为零的数组数组。每当您尝试对其下标时,它都会超出范围,因为零长度数组(无论是什么类型)都没有有效的索引。

【讨论】:

  • 即使将其设为不为零,它仍然会给出相同的错误。在那种情况下,我想创建一个列表,但我也需要它是一个二维数组。
猜你喜欢
  • 2015-03-12
  • 1970-01-01
  • 2021-07-15
  • 1970-01-01
  • 2020-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-24
相关资源
最近更新 更多