gpt4 book ai didi

java - 相邻打印两个二维数组

转载 作者:太空宇宙 更新时间:2023-11-04 14:10:35 25 4
gpt4 key购买 nike

所以我一直在java中摆弄二维数组。我正在练习使用数组作为矩阵。我创建了 2 个 Matrix 对象,它们有一个 myMatrix 2D 数组字段。在 main 方法中,如果可能,我通过 addMatrix 方法调用第一个 Matrix 对象将其自身添加到第二个矩阵。我有一个 printResultDetail 方法来实际打印出正在发生的事情。我希望第一个对象的 myMatrix 以正确格式化的行和列打印出来。然后我希望第二个对象的 myMatrix 对象在前一个对象的右侧打印出来。然后,我希望将由第一个对象的 myResultMatrix 2D 数组给出的新输出矩阵再次打印到右侧。

如何使数组并排打印?

注意:printResultDetail 不正确。我只是想弄清楚该怎么做。

import java.util.Arrays;  
import java.util.Random;

public class Matrix {

int[][] myMatrix;
int[][] myResultMatrix;
int myMatrixRow;
int myMatrixCol;

Random rand = new Random();

public Matrix(int rowSize, int colSize, int maxVal, int minVal){
myMatrixRow = rowSize;
myMatrixCol = colSize;

myMatrix = new int[rowSize][colSize];

for(int i = 0; i < rowSize; i++){
for(int k = 0; k < colSize; k++){
myMatrix[i][k] = rand.nextInt((maxVal - minVal) + 1) + minVal; //assigns each part of array to rand #
}
}

for(int i = 0; i<myMatrixRow; i++){
System.out.print("[");
for(int j = 0; j<myMatrixCol; j++){
System.out.print(" " + myMatrix[i][j] + " ");
}
System.out.print("]");
System.out.println();
}
//System.out.println(Arrays.deepToString(myMatrix));

}

public int[][] multMatrix(Matrix matrix2){

if(canMultiply(matrix2) == true){

myResultMatrix = new int[myMatrixRow][matrix2.myMatrixCol];

for (int i = 0; i < myMatrixRow; i++) {
for (int j = 0; j < matrix2.myMatrixCol; j++) {
for (int k = 0; k < myMatrixCol; k++) {
myResultMatrix[i][j] += myMatrix[i][k] * matrix2.myMatrix[k][j];
}
}
}
return myResultMatrix;
}else{
myResultMatrix = null;
return null;
}

}


public boolean canMultiply(Matrix matrix2){ //can only multiply if the columns of
//first matrix is equal to the rows of the second
if(myMatrixCol == matrix2.myMatrixRow){
return true;
}else{
return false;
}
}

public int[][] addMatrix(Matrix matrix2){
if(myMatrixRow == matrix2.myMatrixRow && myMatrixCol == matrix2.myMatrixCol){

myResultMatrix = new int[myMatrixRow][myMatrixCol];

for(int i = 0; i < myMatrixRow; i++){
for(int k = 0; k < myMatrixCol; k++){
myResultMatrix[i][k] = myMatrix[i][k] + matrix2.myMatrix[i][k];
}
}
return myResultMatrix;
}else{
myResultMatrix = null;
return null;
}
}


public void printResultDetail(Matrix matrix2){

for(int i = 0; i<myMatrixRow; i++){
System.out.print("[");
for(int j = 0; j<myMatrixCol; j++){
System.out.print(" " + myMatrix[i][j] + " ");
}
System.out.print("] ");
System.out.println();

for(int k = 0; k<matrix2.myMatrixRow; k++){
System.out.print("[");
for(int x = 0; x<matrix2.myMatrixCol; x++){
System.out.print(" " + matrix2.myMatrix[k][x] + " ");
}
System.out.print("]");


}

}
}


public static void main(String[] args){
Matrix firstMatrix = new Matrix(3, 3, 5, 1);
Matrix secondMatrix = new Matrix(3, 3, 5, 1);
System.out.println(Arrays.deepToString(firstMatrix.addMatrix(secondMatrix)));
//System.out.println(Arrays.deepToString(firstMatrix.addMatrix(secondMatrix)));
}
}

最佳答案

下面的代码将打印:

matrix side by side

也许你可以用作示例。

public class Test {

public static void main(String[] args) {
double[][] matrixLeft = {{1,5,2,8,4,70,55,80},{3,7,4,2,6,60,30,70}};
double[][] matrixRight = {{8,1,6,4,2,10,40,60},{1,5,2,8,4,70,50,80},{3,7,4,2,6,60,30,70}};

int endOfLoop = matrixLeft.length > matrixRight.length ? matrixLeft.length : matrixRight.length;
for(int i = 0; i < endOfLoop; i++){
if(matrixLeft.length > i){
printLine(matrixLeft[i]);
System.out.print(" ");
} else {
printBlankLine(matrixLeft[0].length);
}

if(matrixRight.length > i){
printLine(matrixRight[i]);
}
System.out.println();
}
}

private static void printLine(double[] line){
for(double number : line){
System.out.print(number + " ");
}
}

private static void printBlankLine(int lenght){
for(int i=0; i < lenght; i++){
System.out.print(" ");
}
}

}

关于java - 相邻打印两个二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28376765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com