java数据结构之矩阵的实现

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/m0_37942202/article/details/82021144

这里是借助Vector,以二维数组的形式实现矩阵,并且完成矩阵相加的运算

import java.util.HashSet;
import java.util.Random;
import java.util.Vector;


public class SetTest {
//  static HashSet s=new HashSet(3);
    public static void main(String[] args)
    {
        Matrix x1=new Matrix(3,4);
        Matrix x2=new Matrix(3,4);
        Random r=new Random();
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<4;j++)
            {
                x1.set(i, j, r.nextInt(10));
            }
        }

        for(int i=0;i<3;i++)
        {
            for(int j=0;j<4;j++)
            {
                x2.set(i, j, r.nextInt(10));
            }
        }
        x1.print();
        System.out.println();
        x2.print();
        System.out.println();

        x1.add(x2).print();
    }
}

class Matrix{
    private Vector values;
    private int row;
    private int col;

    public Matrix(int row,int col)
    {
        values=new Vector(row);
        for(int i=0;i<row;i++)
        {
            Vector c=new Vector(col);
            values.add(c);
            for(int j=0;j<col;j++)
            {
                c.add(null);
            }

        }
        this.row=row;this.col=col;
    }

    public void set(int row,int col,Object value)
    {
        Vector v=(Vector) values.get(row);
        v.set(col, value);

    }

    public Object get(int row,int col)
    {
        Vector v=(Vector) values.get(row);
        return v.get(col);
    }

    public int width()
    {
        return col;
    }

    public int height()
    {
        return row;
    }

    public Matrix add(Matrix x)
    {
        Matrix result=new Matrix(row,col);
        for(int i=0;i<row;i++)
        {
            for(int j=0;j<col;j++)
            {
                Integer integer1=(Integer) this.get(i, j);
                Integer integer2=(Integer) x.get(i, j);
                result.set(i, j, new Integer(integer1.intValue()+integer2.intValue()));
            }
        }
        return result;
    }

    public void print()
    {
        for(int i=0;i<row;i++)
        {
            for(int j=0;j<col;j++)
            {
                System.out.print(this.get(i, j)+" ");
            }
            System.out.println();
        }
    }

}
展开阅读全文

数据结构--矩阵

11-09

[b]以下代码是照着书本编的,但是怎么都看不懂,不知道怎么写主函数,请各位帮个忙.[/b]rn只要实现矩阵的输入即可.rn如果能给点注释就更好了~~~rnrn#include "math.h"rn#include "iostream"rnusing namespace std;rnrnenum BooleanFalse,True;rnstruct Triplernrn int row,col;rn float value;rn;//定义一个三元组rnclass Matrix;rnclass MatrixNodernrn friend class Matrix;rn friend istream &operator>>(istream &,Matrix &);rnpublic:rn MatrixNode *dowm,*right;rn Boolean head;rn unionTriple triple;Matrix *nextrn MatrixNode *next;rn MatrixNode(Boolean b,Triple *t);rn;rnMatrixNode::MatrixNode(Boolean b,Triple *t)rnrn head=b;rn if(b)right=next=this;rn else triple=*t;rn;rntypedef MatrixNode *MatrixNodePtr;rnclass Matrixrnrn friend istream & operator>>(istream &,Matrix &);rnpublic:rn ~Matrix();rn MatrixNode *headnode;rn;rn///////////////////////////////////////////////////////rnistream & operator>>(istream *is,Matrix *matrix)rnrn Triple s;rn int p;rn is>>s.row>>s.col>>s.value;rn if(s.row>s.col)p=s.row;rn else p=s.col;rn matrix.headnode=new MatrixNode(False,&s);rn if(!p)(matrix.headnode)->right=matrix.headnode;return is;rn MatrixNodePtr *H=new MatrixNodePtr(p);//////////////////////////////////////////////////rn for(int i=0;i>t.row>>t.col>>t.value;rn if(t.row>CurrentRow)rn rn last->right=H[CurrentRow];rn CurrentRow=t.row;rn last=H[CurrentRow];rn rn last=last->right=new MatrixNode(False,&t);rn H[t.col]->next=H[t.col]->next->down=last;rn rn last->right=H[CurrentRow];rn for(i=0;inext->down=H[i];rn for(i=0;inext=H[i+1];rn H[p-1]->next=matrix.headnode;rn matrix.headnode->right=H[0];rn delete[]H;rn return is;rnrnrnvoid main()rnrn rn 论坛

没有更多推荐了,返回首页