英文:
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 < arrayValues.length; a++) {
groupedValues[0][a] = arrayValues[a];
}
for (int b = 8; b < arrayValues.length; b++) {
groupedValues[1][b] = arrayValues[b];
}
for (int c = 16; c < 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 < arrayValues.length / 8; i++) {
for (int a = 0; a < 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 < groupSize; i++) {
for (int a = 0; a < 8; a++) {
index = (i * 8) + a;
// if the current index is beyond the input array, add 0
if (index > (arrayValues.length - 1)) {
groupedValues[i][a] = 0;
} else {
groupedValues[i][a] = arrayValues[(i * 8) + a];
}
}
}
System.out.println(Arrays.deepToString(groupedValues));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论