Java 再做一次循环
我有一个2D数组,它具有以下值Java 再做一次循环,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,我有一个2D数组,它具有以下值 1,2,3 1,0,2 0,1,2 以及具有以下值的数组 0,1,2,3,4,5,6,7,8 我想实现的是,它首先从数组[0]开始 matrix[0][0] * array[0] = result1 matrix[0][1] * array[1] = result2 matrix[0][2] * array[2] = result3 matrix[1][0] * array[0] = result4 matrix[1][1] * array[1]
1,2,3
1,0,2
0,1,2
以及具有以下值的数组
0,1,2,3,4,5,6,7,8
我想实现的是,它首先从数组[0]开始
matrix[0][0] * array[0] = result1
matrix[0][1] * array[1] = result2
matrix[0][2] * array[2] = result3
matrix[1][0] * array[0] = result4
matrix[1][1] * array[1] = result5
matrix[1][2] * array[2] = result6
matrix[2][0] * array[0] = result7
matrix[2][1] * array[1] = result8
matrix[2][2] * array[2] = result9
在这之后,它将返回并重做循环,但这次从数组[3]开始
matrix[0][0] * array[3] = result10
matrix[0][1] * array[4] = result11
matrix[0][2] * array[5] = result12
matrix[1][0] * array[3] = result13
matrix[1][1] * array[4] = result14
matrix[1][2] * array[5] = result15
matrix[2][0] * array[3] = result16
matrix[2][1] * array[4] = result17
matrix[2][2] * array[5] = result18
在这之后,它将返回并重做循环,但这次从数组开始[6]
matrix[0][0] * array[6] = result19
matrix[0][1] * array[7] = result20
matrix[0][2] * array[8] = result21
matrix[1][0] * array[6] = result22
matrix[1][1] * array[7] = result23
matrix[1][2] * array[8] = result24
matrix[2][0] * array[6] = result25
matrix[2][1] * array[7] = result26
matrix[2][2] * array[8] = result27
我试过了,但没有达到我想要的效果。请帮忙
公共类Test1{
公共静态void main(字符串[]args){
整数矩阵[][]=新整数[][]{
{1,2,3},
{1,0,2},
{0,1,2}
};
int数组[]={0,1,2,3,4,5,6,7,8};
整数计数=0;
int a=0;
int行=0,列=0;
布尔完成=假;
而(!完成){
对于(行=0;行0){
计数++;
}
如果(计数>列和行!=列){
计数=0;
}
if(行==matrix.length-1&&col==matrix.length-1){
计数=计数+1;
}
系统输出打印项次(计数);
}
}
if(行==matrix.length-1&&col==matrix.length-1){
计数=计数+1;
}
if(count==array.length){
完成=正确;
打破
}
}
}
}
如果我理解您的问题,您可以使用一个简单的for
循环,每次迭代将数组
索引增加3。大概
int[][] matrix = { { 1, 2, 3 }, { 1, 0, 2 }, { 0, 1, 2 } };
int[][] result = new int[matrix.length][];
int[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
int t = 0;
for (int i = 0; i < array.length; i += 3) {
result[t++] = new int[] { matrix[0][0] * array[i],
matrix[0][1] * array[i + 1], matrix[0][2] * array[i + 2],
matrix[1][0] * array[i], matrix[1][1] * array[i + 1],
matrix[1][2] * array[i + 2], matrix[2][0] * array[i],
matrix[2][1] * array[i + 1], matrix[2][2] * array[i + 2] };
}
System.out.println(Arrays.deepToString(result));
int[][]矩阵={{1,2,3},{1,0,2},{0,1,2};
int[][]结果=新的int[matrix.length][];
int[]数组={0,1,2,3,4,5,6,7,8};
int t=0;
对于(int i=0;i
这是您可以使用的代码:它输出每个乘法的结果和相应的结果索引
public static void main(String[] args) {
int matrix[][] = new int[][] { { 1, 2, 3 }, { 1, 0, 2 }, { 0, 1, 2 } };
int array[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
int globalCount = 0;
int count = 0;
int a = 0;
int row = 0, col = 0;
boolean done = false;
for (count = 0; count < array.length; count += 3) {
for (row = 0; row < 3; row++) {
for (col = 0; col < 3; col++) {
a = matrix[row][col] * array[count++];
System.out.println("Result" + globalCount++ + " " + a);
}
count -= 3;
}
}
}
如果你可以重写你的
“我想要实现什么”
部分来使用索引,这将使它更加清晰。具体来说,你所做的尝试有什么问题?您是否收到错误消息?这是一个矩阵乘法运算吗?有没有别的方法可以让我们更容易理解你的问题?我试着重写了。希望现在很清楚,简单,我正在为你写代码。等几分钟
public static void main(String[] args) {
int matrix[][] = new int[][] { { 1, 2, 3 }, { 1, 0, 2 }, { 0, 1, 2 } };
int array[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
int globalCount = 0;
int count = 0;
int a = 0;
int row = 0, col = 0;
boolean done = false;
for (count = 0; count < array.length; count += 3) {
for (row = 0; row < 3; row++) {
for (col = 0; col < 3; col++) {
a = matrix[row][col] * array[count++];
System.out.println("Result" + globalCount++ + " " + a);
}
count -= 3;
}
}
}
Result0 0
Result1 2
Result2 6
Result3 0
Result4 0
Result5 4
Result6 0
Result7 1
Result8 4
Result9 3
Result10 8
Result11 15
Result12 3
Result13 0
Result14 10
Result15 0
Result16 4
Result17 10
Result18 6
Result19 14
Result20 24
Result21 6
Result22 0
Result23 16
Result24 0
Result25 7
Result26 16
public static void multiply(int[][] matrix, int[] array) {
int sum = 0;
int temp = 0;
int k = 0;
int limit = 2;
int end = 0;
do {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
temp = matrix[i][j] * array[k];
sum += temp;
System.out.println("matrix[" + i + "][" + j + "] * array["
+ k + "] = " + sum);
if (k < limit) {
k++;
} else {
k = end;
}
temp = 0;
}
}
System.out.println();
limit += 3;
end += 3;
k = end;
} while (end < 9);
}
matrix[0][0] * array[0] = 0
matrix[0][1] * array[1] = 2
matrix[0][2] * array[2] = 8
matrix[1][0] * array[0] = 8
matrix[1][1] * array[1] = 8
matrix[1][2] * array[2] = 12
matrix[2][0] * array[0] = 12
matrix[2][1] * array[1] = 13
matrix[2][2] * array[2] = 17
matrix[0][0] * array[3] = 20
matrix[0][1] * array[4] = 28
matrix[0][2] * array[5] = 43
matrix[1][0] * array[3] = 46
matrix[1][1] * array[4] = 46
matrix[1][2] * array[5] = 56
matrix[2][0] * array[3] = 56
matrix[2][1] * array[4] = 60
matrix[2][2] * array[5] = 70
matrix[0][0] * array[6] = 76
matrix[0][1] * array[7] = 90
matrix[0][2] * array[8] = 114
matrix[1][0] * array[6] = 120
matrix[1][1] * array[7] = 120
matrix[1][2] * array[8] = 136
matrix[2][0] * array[6] = 136
matrix[2][1] * array[7] = 143
matrix[2][2] * array[8] = 159