我有一个具有以下值的二维数组
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
我尝试了但却无法实现我想要的输出。请帮忙。
public class Test1 {
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 count = 0;
int a = 0;
int row = 0, col = 0;
boolean done = false;
while(!done) {
for (row = 0; row < matrix.length; row++) {
for (col = 0; col < matrix.length; col++) {
a = matrix[row][col] * array[count];
if(col > 0) {
count++;
}
if(count > col && row != col) {
count = 0;
}
if(row == matrix.length-1 && col == matrix.length-1) {
count = count + 1;
}
System.out.println(count);
}
}
if(row == matrix.length-1 && col == matrix.length-1) {
count = count + 1;
}
if(count == array.length) {
done = true;
break;
}
}
}
}
答案 0 :(得分:2)
这是您可以使用的代码:它使用相应的结果索引输出每次乘法的结果。
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
答案 1 :(得分:0)
如果我理解了您的问题,您可以通过在每次迭代中将for
索引递增3来使用简单的array
循环来完成。像,
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));
答案 2 :(得分:0)
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