그래서 2 차원 배열의 테이블을 인쇄하는 코드가 있습니다. 내가 겪은 문제는 배열의 곱을 곱하고 찾는 방법을 전혀 모른다는 것입니다. 도움을 주시면 감사하겠습니다. 감사
public class MultiplyingArrays {
public static void main(String[] args) {
int firstarray[][] = {{1, 2, -2, 0}, {-3, 4, 7, 2}, {6, 0, 3, 1}};
int secondarray[][] = {{-1, 3}, {0, 9}, {1, -11}, {4, -5}};
System.out.println("This is the first array");
display(firstarray);
System.out.println("This is the second array");
display(secondarray);
}
public static void display(int x[][]) {
for (int row = 0; row < x.length; row++) {
for (int column = 0; column < x[row].length; column++) {
System.out.print(x[row][column] + "\t");
}
System.out.println();
}
}
}
원하는 결과는 다음과 같습니다.
-3 43
18 60
1 -20
int firstarray[][] = {{1, 2, -2, 0}, {-3, 4, 7, 2}, {6, 0, 3, 1}};
int secondarray[][] = {{-1, 3}, {0, 9}, {1, -11}, {4, -5}};
/* Create another 2d array to store the result using the original arrays' lengths on row and column respectively. */
int [][] result = new int[firstarray.length][secondarray[0].length];
/* Loop through each and get product, then sum up and store the value */
for (int i = 0; i < firstarray.length; i++) {
for (int j = 0; j < secondarray[0].length; j++) {
for (int k = 0; k < firstarray[0].length; k++) {
result[i][j] += firstarray[i][k] * secondarray[k][j];
}
}
}
/* Show the result */
display(result);
PS 적절한 명명 규칙을 사용합니다 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다