用java 编写一个矩阵类Matrix,要求能利用该矩阵类生成任意行、列的矩阵对象,比如3╳5矩阵,

2024-11-05 16:36:49
推荐回答(2个)
回答(1):

public class Matrix {
private int row=0;
private int col=0;
private int [] [] array = null;
public Matrix () {}
public Matrix (int row, int col){
this.row=row;
this.col=col;
array = new int [row][col];
}
public void showMatrix(){
for(int i = 0 ; i < row; i++){
for(int j = 0; j < col; j++){
array[i][j]=(int)(Math.random()*20)+1;
System.out.print(array[i][j]+"\t");
}
System.out.println();
}
}
public static void main(String[] arge) {
Matrix m1 = new Matrix(3,5);
m1.showMatrix();
System.out.println;
Matrix m2 = new Matrix(10,20);
m2.showMatrix();
}
}

回答(2):

用个二维数组就可以了,何必这么复杂。