简体   繁体   English

用户定义的二维数组的大小(Java)

[英]user defined size of a two dimensional array (Java)

I'm new to java, and i'm trying to make a array who's size is user defined. 我是java的新手,我正在尝试制作一个大小由用户定义的数组。 I'm staring off by filling in the entire array with zeros and moving from there. 我盯着整个零填充整个数组,然后从那里开始。 How should I go about doing that? 我应该怎么做呢? What I have right now is something similar to: (input being my scanner object) 我现在所拥有的类似于:(输入是我的扫描仪对象)

int num1, num2, num3 = 0, num4 = 0, active = 0;

num1 = input.nextInt();
num2 = input.nextInt();

int[][] ver = new int[num1][num2];

while(active == 0){

    ver [num3][num4] = 0;
    ++num4;

    if(num4 > num2){

        ++num3;
        num4 = 0;
    }

    if(num3 > num1){

        ++active

    }

}

This keeps giving me an ArrayIndexOutOfBoundsException:0, making me think ver[0][0] doesn't exist. 这一直给我一个ArrayIndexOutOfBoundsException:0,让我觉得ver [0] [0]不存在。 Thanks in advance. 提前致谢。

There is no checking for the values of num3 and num4 in your code. 在您的代码中不检查num3num4的值。 Add proper restrictions as I envisage, you don't what them to be greater thatn num1 and num2 respectively. 根据我的设想添加适当的限制,您不要将它们分别设置为大于num1num2

If the value of num3 or num4 goes beyond the limit you get the ArrayIndexOutOfBoundException . 如果num3或num4的值超出限制,则将获得ArrayIndexOutOfBoundException

You are not checking num3 and num4 properly (they both are allowed to reach the upper bound) so they will eventually go out of bounds. 您没有正确检查num3num4 (它们都被允许到达上限),因此它们最终将超出范围。

Since you're just trying to fill your array with zeros why don't you consider the following code: 由于您只是想用零填充数组,所以为什么不考虑以下代码:

import java.util.Arrays;

// ...

int num1 = 2; // fixed number just for test purposes
int num2 = 2; // fixed number just for test purposes

int[][] ver = new int[num1][num2];
for (int[] row: ver)
    Arrays.fill(row, 0);

System.out.println(ver[0][0]); // prints 0 as expected

Since every subarray ( row in the example) is an array the above code will loop through them filling them with zeros, taking advantage of the fill method of the Arrays class, which you need to import as shown in the example. 由于每个子数组(示例中的row )都是一个数组,因此上述代码将遍历它们,并利用Arrays类的fill方法将它们填充为零,如示例所示,您需要将其导入。

This fails when num4 == num2 as you are checking num4 > num2 ; num4 == num2时,这将失败,因为您正在检查num4 > num2 because num2 is the length of the array while index goes to length-1 . 因为num2是数组的长度,而index转到length-1 Same goes with num3 and num1 comparison. num3num1比较也是如此。 Use num2-1 and num1-1 in the comparison as below: 在比较中使用num2-1num1-1 ,如下所示:

    int num1=0, num2=0, num3=0,num4=0;
    num1 = input.nextInt();  //<--Input a value >0
    num2 = input.nextInt();  //<--Input a value >0
    int[][] ver = new int[num1][num2];
    int active = 0;
    while(active == 0){
        ver[num3][num4] = 0;
        ++num4;
        if(num4 > num2-1){     // or if(num4 >= num2)
            ++num3;
            num4 = 0;
        }
        if(num3 > num1-1){     // or if(num3 >= num1)
            ++active;
        }
    }

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

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