📄 matrix.java
字号:
public class Matrix{
private int h;//成员变量,矩阵行数
private int w;//成员变量,矩阵列数
private int[][] matrix;
public Matrix(int h,int w){//构造函数,h为行数,w为列数
if(!(h > 0 && w > 0))
throw new ArrayIndexOutOfBoundsException("h or w < " + 1);
matrix = new int[h][w]; //创建有h行的对象
this.h = h;
this.w = w;
}
public void set(int row,int col,int value){//置元素
if(!(row >= 0 && w >= 0 && row < h && col <w))
throw new ArrayIndexOutOfBoundsException("h or w < " + "-1");
matrix[row][col]=(int)value;
}
public Object get(int row,int col){ //取元素
if(!(row >= 0 && w >= 0 && row < h && col <w))
throw new ArrayIndexOutOfBoundsException("h or w < " + "-1");
return matrix[row][col];
}
public int width(){ //矩阵列数
return w;
}
public int height(){//矩阵行数
return h;
}
public Matrix add(Matrix b){//矩阵加
if(height() != b.height() || width()!= b.width()){
throw new ArrayIndexOutOfBoundsException("Matrix h and w error");
}
Matrix result = new Matrix(height(),width());
for(int i = 0;i < height();i ++){
for(int j = 0;j < width(); j ++){
Integer i1 = (Integer)get(i,j);
Integer i2 = (Integer)(b.get(i,j));
result.set(i,j,new Integer(i1.intValue()+i2.intValue()));
}
}
return result;
}
public void print(){//输出矩阵元素
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
System.out.print(matrix[i][j]+"\t");
}
System.out.println();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -