java.lang.ArrayIndexOutOfBoundsException – 从另一个数组循环填充新数组

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

java.lang.ArrayIndexOutOfBoundsException - Fill new Array with looped Data from other Array

问题

我有一个扫描器,它设置了数组长度,然后用0到1000之间的随机数填充数组。

接下来,我想要获取所有偶数,并将它们存储在一个新的数组中。为此,我创建了一个循环来计算新数组的大小。

其次,我再次运行循环,并希望从原始数组中填充新数组中的所有偶数。

如果我测试长度为1或2,代码有时可以工作,并且可以生成新数组。如果我使用长度为10的数组,我会得到一个错误,类似于:

java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 4

对于这个错误示例,我设置了一个长度为10的数组。我在其中得到了4个偶数。计数工作正常,并且变为“4”。然而,我收到了一个错误。

    // 获取数组长度的计数
	int count = 0;
	for(int i = 0; i < arrayLenght; i++){
		if (dasArray[i] % 2 == 0) {
			count++;
		}
	}
	
	System.out.println("COUNT " + count);
	
	
	// 使用计数创建新的偶数数组
	int []dasGeradeArray = new int[count];
	for(int i = 0; i < arrayLenght; i++){
		if (dasArray[i] % 2 == 0) {
			dasGeradeArray[i]= dasArray[i];
		}
	}
英文:

I have a Scanner which sets the arrayLenght, then the array is filled with random numbers between 0 and 1000.

Next, I want to get all even numbers and store them in a new array. For that, I created a loop to count the size of the new array.

Second, I run the loop again and want to fill the new array with all even numbers from the original array.

If I test with a length of 1 or 2, the code sometimes works and the new array can be generated. If I work with an array lenght of like 10, I get an Error like:

java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 4

For this Error example I have set up an array with a length of 10. I got 4 Even Numbers in it. The Count worked and changed to "4". Yet I get an Error.

    //GET A COUNT FOR THE LENGHT OF ARRAY
	int count = 0;
	for(int i = 0; i &lt; arrayLenght; i++){
		if (dasArray[i] % 2 == 0) {
			count++;
		}
	}
	
	System.out.println(&quot;COUNT &quot; + count);
	
	
	//CREATE NEW ARRAY WITH ALL EVEN NUMBERS AND THE LENGHT OF COUNT
	int []dasGeradeArray = new int[count];
	for(int i = 0; i &lt; arrayLenght; i++){
		if (dasArray[i] % 2 == 0) {
			dasGeradeArray[i]= dasArray[i];
		}
	}

答案1

得分: 1

这是因为dasGeradeArray的索引数量只有dasArray的一半。你不应该使用相同的i变量。你可以添加另一个变量,每次向dasGeradeArray中添加内容时递增:

// 用所有偶数创建一个新数组,并计算计数的长度
int[] dasGeradeArray = new int[count];
int dasGeradeCounter = 0;
for (int i = 0; i < arrayLenght; i++) {
    if (dasArray[i] % 2 == 0) {
        dasGeradeArray[dasGeradeCounter] = dasArray[i];
        dasGeradeCounter++;
    }
}
英文:

This is because the dasGeradeArray has half of the indexes that dasArray has. You shouldn't use the same i variable. You can add another variable that you increment each time you add something to dasGeradeArray:

/

/CREATE NEW ARRAY WITH ALL EVEN NUMBERS AND THE LENGHT OF COUNT
    int []dasGeradeArray = new int[count];
    int dasGeradeCounter = 0
    for(int i = 0; i &lt; arrayLenght; i++){
        if (dasArray[i] % 2 == 0) {
            dasGeradeArray[dasGeradeCounter]= dasArray[i];
            dasGeradeCounter++;
        }

huangapple
  • 本文由 发表于 2020年9月30日 20:08:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64137249.html
匿名

发表评论

匿名网友

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

确定