将普通数组中的值分组并移动到多维数组中。

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

group values in normal array and move to multidimensional array

问题

arrayValues = new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
int[][] groupedValues = new int[3][8];

for (int a = 0; a < arrayValues.length; a++) {
    groupedValues[0][a % 8] = arrayValues[a];
}
for (int b = 8; b < arrayValues.length; b++) {
    groupedValues[1][b % 8] = arrayValues[b];
}
for (int c = 16; c < arrayValues.length; c++) {
    groupedValues[2][c % 8] = arrayValues[c];
}

结果应为:
groupedValues = [[1, 1, 1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3, 3, 3]]

但实际输出为:
groupedValues = [[1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]

由于某种原因,它未能对接下来的值进行分组,并将它们添加到groupedValues[]数组中。

英文:

I'm struggling to create multi dimensional array by grouping the arrayValues[] in groups of 8 and copying them to the dimensional array. Any help will be appreciated. The code does not give me the right output.

arrayValues = new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
int[][] groupedValues =  new int[3][8];
		
for (int a = 0; a &lt; arrayValues.length; a++) {
	groupedValues[0][a] = arrayValues[a];
}
for (int b = 8; b &lt; arrayValues.length; b++) {
	groupedValues[1][b] = arrayValues[b];
}
for (int c = 16; c &lt; arrayValues.length; c++) {
	groupedValues[2][c] = arrayValues[c];
}

The result should be:

groupedValues = [[1, 1, 1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3, 3, 3]]

But it gives the output:

groupedValues = [[1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]

For some reason it doesn't group the next values and add them to the groupedValues[] array.

答案1

得分: 1

正如评论中已经说明的那样,当向数组添加值时,你的代码肯定会抛出 IndexOutOfBoundsException 异常。这是因为你在所有三个循环中都遍历完整的输入数组,并试图将 24 个值放入大小为 8 的子数组中。

以下代码应该适用于你:

public static void main(String[] args) {
    int[] arrayValues = new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
    int[][] groupedValues = new int[arrayValues.length / 8][8];

    for (int i = 0; i < arrayValues.length / 8; i++) {
        for (int a = 0; a < 8; a++) {
            groupedValues[i][a] = arrayValues[(i * 8) + a];
        }
    }
    System.out.println(Arrays.deepToString(groupedValues));
}

编辑:

如果你的输入数组无法分割成大小为 8 的组,以下代码应该适用:

public static void main(String[] args) {
    int[] arrayValues = new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3 };
    // 计算应该有多少个可用的大小为 8 的组
    int groupSize = (int) Math.ceil(arrayValues.length / 8.0); 
    int[][] groupedValues = new int[groupSize][8];
    int index;

    for (int i = 0; i < groupSize; i++) {
        for (int a = 0; a < 8; a++) {
            index = (i * 8) + a;
            // 如果当前索引超出了输入数组,添加 0
            if (index > (arrayValues.length - 1)) {
                groupedValues[i][a] = 0;
            } else {
                groupedValues[i][a] = arrayValues[(i * 8) + a];
            }
        }
    }
    System.out.println(Arrays.deepToString(groupedValues));
}
英文:

As already stated in the comments, your code should have surely given you an IndexOutOfBoundsException when adding the values to the array.
This is because you iterate over the complete input array in all three loops and trying to fit 24 values into an sub array of size 8.

This code should work for you:

public static void main(String[] args) {
    int[] arrayValues = new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
    int[][] groupedValues = new int[arrayValues.length / 8][8];

    for (int i = 0; i &lt; arrayValues.length / 8; i++) {
        for (int a = 0; a &lt; 8; a++) {
            groupedValues[i][a] = arrayValues[(i * 8) + a];
        }
    }
    System.out.println(Arrays.deepToString(groupedValues));
}

Edit:

In case your input array is not splittable into groups of 8, this should work:

public static void main(String[] args) {
    int[] arrayValues = new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3 };
    // Calculate how many groups of 8 there should be available
    int groupSize = (int) Math.ceil(arrayValues.length / 8.0); 
    int[][] groupedValues = new int[groupSize][8];
    int index;

    for (int i = 0; i &lt; groupSize; i++) {
        for (int a = 0; a &lt; 8; a++) {
            index = (i * 8) + a;
            // if the current index is beyond the input array, add 0
            if (index &gt; (arrayValues.length - 1)) {
                groupedValues[i][a] = 0;
            } else {
                groupedValues[i][a] = arrayValues[(i * 8) + a];
            }
        }
    }
    System.out.println(Arrays.deepToString(groupedValues));
}

huangapple
  • 本文由 发表于 2020年9月8日 14:46:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63788448.html
匿名

发表评论

匿名网友

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

确定