矩阵是工程设计中经常使用的数学工具。
矩阵用两维数组处理最为方便。
二维数组存储结构
package cn.matrix;
/**
*
*矩阵类
*/
public class Matrix{
//成员变量,矩阵元素
private MyVector values;
//成员变量,矩阵行数
private int h;
//成员变量,矩阵列数
private int w;
//构造函数,h为行数,w为列数
public Matrix(int h,int w){
if(!(h > 0 && w > 0))
throw new ArrayIndexOutOfBoundsException("h or w < " + 1);
//创建有h行的对象
values = new MyVector(h);
for(int i = 0; i < h; i++){
//创建有w列的对象
MyVector row = new MyVector(w);
//让行元素引用等于row
values.add(row);
for(int j = 0; j < w; j++){
row.add(null);//初始化矩阵元素为null
}
}
this.h = h;
this.w = w;
}
//置元素
public void set(int row,int col,Object value){
if(!(row >= 0 && w >= 0 && row < h && col <w))
throw new ArrayIndexOutOfBoundsException("h or w < " + "-1");
MyVector selrow = (MyVector)values.get(row);
selrow.set(col,value);
}
//取元素
public Object get(int row,int col){
if(!(row >= 0 && w >= 0 && row < h && col <w))
throw new ArrayIndexOutOfBoundsException("h or w < " + "-1");
MyVector selrow = (MyVector)values.get(row);
return selrow.get(col);
}
//矩阵列数
public int width(){