为什么在尝试输出数组时会出现异常错误?

huangapple go评论72阅读模式
英文:

Why do I get an exception error when I try output an array?

问题

我编写了一个简单的代码,应该会输出一个2D数组。
这是代码:

String month[];
int speedfines[][];

public int speedFines() {
    speedfines = new int[4][3];
    month = new String[3];

    month[0] = "JAN";
    month[1] = "FEB";
    month[2] = "MAR";

    speedfines[0][0] = 128;
    speedfines[0][1] = 135;
    speedfines[0][2] = 139;
    speedfines[1][0] = 155;
    speedfines[1][1] = 129;
    speedfines[1][2] = 175;
    speedfines[2][0] = 129;
    speedfines[2][1] = 130;
    speedfines[2][2] = 185;
    speedfines[3][0] = 195;
    speedfines[3][1] = 155;
    speedfines[3][2] = 221;

    System.out.println(Arrays.deepToString(speedfines));

    return 0;
}

当我运行这段代码时,它给我一个Java异常线程错误。
我使用的是NetBeans 12.0,我的代码中没有任何错误,但当我运行时,我得到了异常错误。
有人可以解释一下这个Java异常是什么意思,如果可能的话,如何修复它。

英文:

I have written a simple code that is supposed to output a 2D array.
This is the code:

String month[];
int speedfines[][];

public int speedFines() {
    speedfines = new int[3][2];
    month = new String[2];

    month[0] = "JAN";
    month[1] = "FEB";
    month[2] = " MAR";

    speedfines[0][0] = 128;
    speedfines[0][1] = 135;
    speedfines[0][2] = 139;
    speedfines[1][0] = 155;
    speedfines[1][1] = 129;
    speedfines[1][2] = 175;
    speedfines[2][0] = 129;
    speedfines[2][1] = 130;
    speedfines[2][2] = 185;
    speedfines[3][0] = 195;
    speedfines[3][1] = 155;
    speedfines[3][2] = 221;

    System.out.println(Arrays.toString(speedfines));

    return 0;
}

When I run this code it gives me java exception in thread error.
I am using netbeans 12.0 and I do not have any errors in my code but when I run I get the exception error error
Can someone explain to me what the java exception means and how to fix it if possible.

答案1

得分: 2

问题在于您为数组monthspeedfines定义的大小。

例如,您将大小定义为2,并尝试将3个元素放入month中。为了使您的代码正常工作,请将数组定义更改为:

speedfines = new int[4][3];
month = new String[3];
 -------------------
|      |      |     |     ⇒    大小 = 3  /  month = new String [3]
 -------------------
   ↑       ↑     ↑
   0       1     2

英文:

The problem is the sizes you defined for the arrays month and speedfines.

For example, you defined size 2 to month and tryed to put 3 elements into it. To your code work properly, change the arrays definitions to:

speedfines = new int [4][3];
month = new String [3];
 -------------------
|      |      |     |     ⇒    Size = 3  /  month = new String [3]
 -------------------
   ↑       ↑     ↑
   0       1     2

答案2

得分: 0

您的 month 数组应该是:

month = new String[3];

当您将其设置为 new String[2] 时,您允许数组中有 2 个项目。

由于您在数组中设置了 3 个项目:

month[0] = "JAN";
month[1] = "FEB";
month[2] = "MAR";

这将引发 ArrayIndexOutOfBoundsException,因为在这一点上索引 2 不存在。

此外,您的数组是在 函数外部 定义的。将它们移到函数内部。

代码应为:

public int speedFines() {
    int speedfines[][] = new int[3][2];
    String month[] = new String[3];

    month[0] = "JAN";
    month[1] = "FEB";
    month[2] = "MAR";

    speedfines[0][0] = 128;
    speedfines[0][1] = 135;
    speedfines[0][2] = 139;
    speedfines[1][0] = 155;
    speedfines[1][1] = 129;
    speedfines[1][2] = 175;
    speedfines[2][0] = 129;
    speedfines[2][1] = 130;
    speedfines[2][2] = 185;
    speedfines[3][0] = 195;
    speedfines[3][1] = 155;
    speedfines[3][2] = 221;

    System.out.println(Arrays.toString(speedfines));

    return 0;
}
英文:

Your month array should be:

month = new String[3];

When you set it to new String[2], you are allowing 2 items to be in the array.

Since you set 3 items in the array:

month[0] = "JAN";
month[1] = "FEB";
month[2] = "MAR";

It raises the ArrayIndexOutOfBoundsException, because at this point index 2 does not exist.

Furthermore, your arrays are defined outside the function. Move them inside the function.

The code should be:

public int speedFines() {
    int speedfines[][] = new int[3][2];
    String month[] = new String[3];

    month[0] = "JAN";
    month[1] = "FEB";
    month[2] = "MAR";

    speedfines[0][0] = 128;
    speedfines[0][1] = 135;
    speedfines[0][2] = 139;
    speedfines[1][0] = 155;
    speedfines[1][1] = 129;
    speedfines[1][2] = 175;
    speedfines[2][0] = 129;
    speedfines[2][1] = 130;
    speedfines[2][2] = 185;
    speedfines[3][0] = 195;
    speedfines[3][1] = 155;
    speedfines[3][2] = 221;

    System.out.println(Arrays.toString(speedfines));

    return 0;
}

答案3

得分: 0

你在二维数组中保存了12个项目,因此您需要执行以下操作 speedfines = new int [4][3];
我认为异常名称是ArrayIndexOutOfBounds,当您给数组指定一个大小并且给它比其大小更大的项目时,会显示该异常。

英文:

you save 12 items in array 2dimention so you need to do this speedfines = new int [4][3];
I think the exception name is ArrayIndexOutOfBounds that show when you put a size of array and give it items bigger than her size

huangapple
  • 本文由 发表于 2020年10月5日 03:54:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64199246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定