简体   繁体   English

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

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

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.当我运行此代码时,它在线程错误中给了我 java 异常。 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.我正在使用 netbeans 12.0 并且我的代码中没有任何错误,但是当我运行时出现异常错误错误 有人可以向我解释 java 异常的含义以及如何修复它(如果可能)。

The problem is the sizes you defined for the arrays month and speedfines .问题是您为数组monthspeedfines定义的大小。

For example, you defined size 2 to month and tryed to put 3 elements into it.例如,您将大小2定义为并尝试将3 个元素放入其中。 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

Your month array should be:你的month数组应该是:

month = new String[3];

When you set it to new String[2] , you are allowing 2 items to be in the array.当您将其设置为new String[2] ,您允许数组中有2项目。

Since you set 3 items in the array:由于您在数组中设置了3项目:

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

It raises the ArrayIndexOutOfBoundsException , because at this point index 2 does not exist.它引发ArrayIndexOutOfBoundsException ,因为此时索引2不存在。

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;
}

you save 12 items in array 2dimention so you need to do this speedfines = new int [4][3];您在数组 2dimention 中保存了 12 个项目,因此您需要执行此操作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我认为异常名称是 ArrayIndexOutOfBounds 显示当您放置数组大小并为其提供大于她的大小的项目时

暂无
暂无

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

相关问题 为什么我在 Java 中尝试通过数组方式打印 ASCII 表时得到空白 output? - Why do I get a blank output when I try to print ASCII table by array approach in Java? 当我尝试查找刚刚放入哈希图中的值时,为什么会出现运行时异常? - Why do I get a Runtime exception when I try to find values I just put in the hashmap? 为什么当我尝试在servlet映射开始时不带“ /”的情况下出现异常 - Why do I get an exception when I try a servlet mapping without the “/” in the beginning 当我尝试编译我的 Java 代码时,为什么会得到“异常;必须被捕获或声明被抛出”? - Why do I get "Exception; must be caught or declared to be thrown" when I try to compile my Java code? 为什么在尝试启动新的Intent时出现异常? - Why I get an exception when I try to start a new Intent? 尝试存储字节数组时,为什么会出现java.lang.NullPointerException? - Why do I get a java.lang.NullPointerException when I try to store a Array of Bytes? 当我尝试在库存外部/没有物品的库存中单击时,为什么会出现错误? - Why do I get an error when I try to click outside the inventory / in the inventory where there's no item 当我尝试在基类方法中调用子类方法时,为什么会出现错误? - Why do I get an error when I try to call a subclass method inside a baseclass method? 当我尝试保存测试计划时,为什么会出现 NoClassDefFound 错误? - Why do I get a NoClassDefFound error when I try to save my test plan? 当我尝试使用具有相同名称和参数类型的两个方法时,为什么会出现编译错误? - Why do I get a compilation error when I try to have two methods with the same name and parameter type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM