0

这个错误是什么意思?java.lang.ArrayIndexOutOfBoundsException: -1 ?

java.lang.ArrayIndexOutOfBoundsException: -1
    at Game.Game.plantVegetables(Game.java:1160)
    at Game.__SHELL11.run(__SHELL11.java:8)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at bluej.runtime.ExecServer$3.run(ExecServer.java:725)

异常发生在:

   Scanner keyIn = new Scanner (System.in);

   for(int leftToPlant=10; leftToPlant>0; leftToPlant--)
      if (field[row1][column1].equals("t") ||
          field[row1][column1].equals("c") ||
          field[row1][column1].equals("p") ||
          field[row1][column1].equals("r"))
4

2 回答 2

2

您正在尝试获取不存在的数组的第 -1 个元素。发布更多代码以获得更准确的答案。

于 2013-11-07T13:49:16.640 回答
0

假设您在 for 循环中有一个数组,您正试图访问数组中具有无效索引的元素。检查您的 for 循环并确保您的数组包含 11 个元素。第一个元素将不会被访问。

包含 10 个元素的数组的 JavaScript 示例代码

    cars=["BMW","Volvo","Saab","Ford","SD","BMW","Volvo","Saab","Ford","SD"];

    for (var i=10;i>0;i--)
    {
     document.write(cars[i]);
    }

输出:

     undefined    - Element 10 
     SD           - Element 9 
     Ford         - Element 8
     Saab
     Volvo
     BMW
     SD
     Ford
     Saab          - Element 2
     Volvo         - Element 1

  Element 0 won't be access because your loop stops at 1.

小提琴:http: //jsfiddle.net/VFLLN/

于 2013-11-07T14:30:21.553 回答