简体   繁体   English

Java数组-初始化的数组超出范围的异常

[英]Java array - Out of bounds exception on an initialized array

I'm getting an out of bounds exception on the "calendarTable[i][j] = str;" 我在“ calendarTable [i] [j] = str;”上遇到了异常。 line below. 下面的行。 I find this funny because I'm initializing it to 15, and I only iterate to 14. I've tried initializing to 20000 and no matter how large of an array I make, it still gets an index out of bounds error. 我觉得这很有趣,因为我将其初始化为15,而仅迭代为14。我尝试将其初始化为20000,无论我创建的数组有多大,它仍然会出现索引超出范围的错误。

public static void main(String[] args) {
        Calendar cal = new GregorianCalendar();
        cal.set(2012, 2, 1);
        cal.set(Calendar.DAY_OF_WEEK, 1);
        int dayOfMonth = 1;
        Object[][] calendarTable = new Object[15][15];
        calendarTable[0] = new String[]{"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};
        for (int i = 0; i < 14; i++) {
            for (int j = 0; j < 14; j++) {
                //calendarTable[i][j] = dayOfMonth++;
                if(i%2 == 0){
                    String str = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
                    calendarTable[i][j] = str;
                    cal.add(Calendar.DAY_OF_YEAR, 1);
                }else{
                    calendarTable[i][j] = dayOfMonth;
                }
                calendarTable[i * 2 + 1][j] = "TEST";
            }
        }
        for (int i = 0; i < 14; i++) {
            for (int j = 0; j < 14; j++) {
                System.out.print(calendarTable[i][j]);
            }
            System.out.println("");
        }
    }

You have initialized it to 15*15 , but then promptly replaced the initial entry with a seven-item array. 您已将其初始化为15*15 ,但随后立即将初始条目替换为七个项目的数组。 By the time you get into the iteration, the array at the element zero has only seven items, not fifteen. 到您进行迭代时,元素零处的数组只有七个项目,而不是十五个。 If you would like to keep it 15*15 , copy the names of the days into the array of 15 items. 如果您希望将其保留为15*15 ,请将日期名称复制到15个项目的数组中。

System.arrayCopy(
    new String[]{"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"}
,   0
,   calendarTable[0]
,   0
,   7);

Also if you would like the index to go full length of the array, use i != 15 , i < 15 , or i <= 14 . 另外,如果您希望索引达到数组的整个长度,请使用i != 15i < 15i <= 14

Because you're running your loop until index < 14 因为您正在循环运行直到index < 14

However you have only 7 days in 2nd dimension. 但是,您在第二维上只有7天。

FIX: If you change all occurrences of 14 to 7 then this exception will be fixed however you will have still make sure whether your required output is coming or not. FIX:如果将所有出现的次数都更改为147则此异常将得到解决,但是您仍将确保所需的输出是否到来。

Because CalendarTable only has 6 indices and it dies when it goes above that value. 因为CalendarTable仅具有6个索引,并且超过该值它将死亡。

if (i % 2 == 0)
        {
            String str = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
            System.out.println("current value of j: " + j);
            calendarTable[i][j] = str;
            cal.add(Calendar.DAY_OF_YEAR, 1);
        }

current value of j: 0
current value of j: 1
current value of j: 2
current value of j: 3
current value of j: 4
current value of j: 5
current value of j: 6
current value of j: 7 //out of bounds

Your problem is here 你的问题在这里

calendarTable[i * 2 + 1][j]

if i is equals to 8 then 8 * 2 + 1 = 17 giving you the exception 如果i等于8,则8 * 2 +1 = 17给您例外

calendarTable [0] = 7个元素的数组大小({“ SUNDAY”,“ MONDAY”,“ TUESDAY”,“ WEDNESDAY”,“ THURSDAY”,“ FRIDAY”,“ SATURDAY”})是否要分配一个值最多写15个项目

It's because of the line: 这是因为行:

calendarTable[0] = new String[]{"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};

calendarTable is an array of arrays. calendarTable是一个数组数组。 Each row does not necessarily need to have the same number of entries as the one above it. 每一行不一定需要具有与其上方相同的条目数。 Thus, when you actually do calendarTable[0] = new String[] , you're telling Java to replace the array at calendarTable[0] with a new one that only has 7 entries, which is why it goes over bounds. 因此,当您实际执行calendarTable[0] = new String[] ,您是在告诉Java用只有7个条目的新数组替换calendarTable [0]处的数组,这就是为什么它超出界限的原因。

Theoretically, nothing is stopping you from also doing this: 从理论上讲,没有什么可以阻止您执行此操作:

   calendarTable[1] = new String[]{"One Entry"};

It's legal. 是合法的 calendarTable[0].length is 7, and calendarTable[1].length is 1. calendarTable[0].length为7, calendarTable[1].length为1。

I don't know what you're trying to achieve in this program, but you either need to do something like this: 我不知道您要在此程序中实现什么,但是您要么需要执行以下操作:

calendarTable[0][0] = "SUNDAY";
calendarTable[0][1] = "MONDAY";
calendarTable[0][2] = "TUESDAY";
calendarTable[0][3] = "WEDNESDAY";
calendarTable[0][4] = "THURSDAY";
calendarTable[0][5] = "FRIDAY";
calendarTable[0][6] = "SATURDAY";

or something like this: 或类似这样的东西:

calendarTable[0] = new String[]{"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "", "", "", "", "", "", "", "", ""};

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM