矩陣用兩維數組處理最爲方便。
二維數組存儲結構
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(){
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(get(i,j)+" ");//(get(i,j)返回Object,不需要調用toString()函數??
}
System.out.println();
}
}
}
package cn.matrix;
/**
*
*矩陣測試類.
*/
public class TestMatrix {
public static void main(String[] args) {
// 創建第一個矩陣
Matrix mt1 = new Matrix(3, 4);
for (int i = 0; i < mt1.height(); i++) {
for (int j = 0; j < mt1.width(); j++) {
mt1.set(i, j, new Integer(i + j)); // 給矩陣元素賦值
}
}
// 創建第二個矩陣
Matrix mt2 = new Matrix(3, 4);
for (int i = 0; i < mt2.height(); i++) {
for (int j = 0; j < mt2.width(); j++) {
mt2.set(i, j, new Integer((int) (Math.random() * 10)));// 調用隨機數函數產生隨機數給矩陣元素賦值
}
}
System.out.println("Matrix 1:");
mt1.print(); // 輸出第一個矩陣的元素
System.out.println("Matrix 2:");
mt2.print(); // 輸出第二個矩陣的元素
Matrix mt3 = mt2.add(mt1); // 矩陣加
System.out.println("results after adding :");
mt3.print(); // 輸出矩陣加後的元素
}
}
package cn.matrix;
public class MyVector{
private Object[] elementData;
private int elementCount;
private void ensureCapacity(int minCapacity){
int oldCapacity = elementData.length;
if (minCapacity > oldCapacity) {
Object oldData[] = elementData;
int newCapacity = oldCapacity * 2;
if (newCapacity < minCapacity) {
newCapacity = minCapacity;
}
elementData = new Object[newCapacity];
System.arraycopy(oldData, 0, elementData, 0, elementCount);
}
}
public MyVector(){
this(10);
}
public MyVector(int initialCapacity){
elementData = new Object[initialCapacity];
elementCount = 0;
}
public void add(int index,Object element){
if (index >= elementCount + 1) {
throw new ArrayIndexOutOfBoundsException(index + " > " + elementCount);
}
ensureCapacity(elementCount + 1);
System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
elementData[index] = element;
elementCount++;
}
public void add(Object element){
add(elementCount,element);
}
public void set(int index,Object element){
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
}
elementData[index] = element;
}
public Object get(int index){
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
return elementData[index];
}
public int size(){
return elementCount;
}
public int indexOf(Object element){
if (element == null) {
for (int i = 0 ; i < elementCount ; i++)
if (elementData[i] == null) return i;
} else {
for (int i = 0 ; i < elementCount ; i++)
if (element.equals(elementData[i]))
return i;
}
return -1;
}
public boolean contain(Object element){
return indexOf(element) >= 0;
}
public void remove(Object element){
int i = indexOf(element);
if (i >= 0) {
remove(i);
}
}
public void remove(int index){
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
}
else if (index < 0) {
throw new ArrayIndexOutOfBoundsException(index);
}
int j = elementCount - index - 1;
if (j > 0) {
System.arraycopy(elementData, index + 1, elementData, index, j);
}
elementCount--;
elementData[elementCount] = null;
}
// public void trimToSzie(){
// int oldCapacity = elementData.length;
// if (elementCount < oldCapacity) {
// Object oldData[] = elementData;
// elementData = new Object[elementCount];
// System.arraycopy(oldData, 0, elementData, 0, elementCount);
// }
// }
// public Object[] toArray(){
// Object[] result = new Object[elementCount];
// System.arraycopy(elementData, 0, result, 0, elementCount);
// return result;
// }
/*
public void removeAll(){
for (int i = 0; i < elementCount; i++)
elementData[i] = null;
elementCount = 0;
}
*/
// public static void vectorCopy(MyVector src,int possrc,MyVector dst,int posdst,int count){
// int length1 = src.size();
// int length2 = dst.size();
// if(length1 - possrc < count || length2 - posdst < count)
// throw new ArrayIndexOutOfBoundsException(count);
// for(int i = 0;i < src.size();i ++){
// dst.add(src.get(i));
// }
// }
// public String toString(){
// String str="";
// for(int i = 0;i < elementCount;i ++){
// str = str + get(i).toString() + " ";
// }
// return str;
// }
}
測試結果:
Matrix 1:
0 1 2 3
1 2 3 4
2 3 4 5
Matrix 2:
3 2 3 1
1 8 4 9
8 3 5 2
results after adding :
3 3 5 4
2 10 7 13
10 6 9 7