【数据结构与算法】矩阵

本文介绍了如何使用二维数组来处理矩阵,并展示了矩阵加法的测试结果,包括两个矩阵的元素及相加后的结果矩阵。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

矩阵是工程设计中经常使用的数学工具。 

矩阵用两维数组处理最为方便。 

二维数组存储结构

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 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值