(1) 设计和编写代表矩阵的Matrix类。该类包括矩阵行列数变量int rows和int cols,矩阵数据数组double data[][],构造方法Matrix()、Matrix(int rows,int cols)、Matrix(int rows,int cols,double data[][]),获取某元素值的方法getData(int row,int col),设置某元素值的方法setData(int row,int col,double value),计算两个矩阵的乘积的方法multiply(Matrix m)以及toString()等内容。
(2) 编写测试类MatrixTest,并在该类中创建两个矩阵对象,计算其乘积。
(1) 编写Matrix类
(2) 编写MatrixTest类。在该类中通过键盘输入方式确定所要创建的两个矩阵的行列数,根据行列数随机生成数据或键盘输入,并通过setData方法生成矩阵的内容。
(3) 计算矩阵的乘积,并把结果通过toString方法输出到屏幕上
//import java.util.Scanner;
class Matrix
{
int rows;
int cols;
double data[][];
void Matrix()
{
rows=0;
cols=0;
data=new double[rows][cols];
}
void Matrix(int rows,int cols)
{
this.rows=rows;
this.cols=cols;
data=new double[this.rows][this.cols];
}
void Matrix(int rows,int cols,double data[][])
{
this.rows=rows;
this.cols=cols;
data=new double[this.rows][this.cols];
//this.data=data[rows][cols];
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++)
{
this.data[i][j]=data[i][j];
}
}
double getData(int row,int col)
{
//for(int i=0;i<row;i++)
//for(int j=0;j<col;j++)
//{
//data[row][col]
//}
data=new double[row][col];
return data[row][col];
}
void setData(int row,int col,double value)
{
data=new double[row][col];
this.data[row][col]=value;
}
double[][] multiply(Matrix m)
{
Matrix n=new Matrix();
for(int i=0;i<m.rows;i++){
for(int j=0;j<this.cols;j++){
n.data[i][j]=0;
for(int k=0;k<m.cols;k++)
n.data[i][j]+=m.data[i][k]*this.data[k][j];
}
}
return n.data;
}
/*
String toString(Matrix m,Matrix n)
{
for(int i=0;i<m.rows;i++)
for(int j=0;j<this.cols;j++)
return n.data[i][j]+" ";
return " ";
}*/
void print(Matrix n)
{
for(int i=0;i<n.rows;i++)
for(int j=0;j<n.cols;j++){
System.out.print(n.data[i][j]+" ");
System.out.println();}
}
}
class MatrixTest
{
public static void main(String[] args)
{
System.out.println("helloworld");
Matrix x=new Matrix();
Matrix k=new Matrix();
x.Matrix(2,3);
System.out.println("helloworld");
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
{
x.setData(i,j,(i+1)*(j+2));
}
System.out.println("helloworld");
double b[][]={{1,5,2,8},{5,9,10,-3},{2,7,-5,-18}};
Matrix y=new Matrix();
y.Matrix(3,4,b);
k.data=y.multiply(x);
y.print(k);
}
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:0
at Matrix.setData(MatrixTest.java:41)
at MatrixTest.main(MatrixTest.java:85)
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
{
x.setData(i,j,(i+1)*(j+2));
}
这样赋值难道有错误码?
void setData(int row,int col,double value)
{
data=new double[row][col];
this.data[row][col]=value;
}
还是这个函数写错了啊?
9 个解决方案
#1
我们下个星期就要交,为什么没有高手来帮我解决啊?????
#2
好像不可以通过形参来给定义数组长度。在定义的时候应该指定
#3
void setData(int row,int col,double value)
{
this.rows=row;
this.cols=col;
data=new double[this.rows][this.cols];
this.data[this.rows][this.cols]=value;
}
那这样写为什么还不对啊???
{
this.rows=row;
this.cols=col;
data=new double[this.rows][this.cols];
this.data[this.rows][this.cols]=value;
}
那这样写为什么还不对啊???
#4
Matrix setData(int row,int col,double value)
{
//this.rows=row;
//this.cols=col;
Matrix A=new Matrix();
A.data[row][col]=value;
return A;
}
将函数改成现在这个形式,
为什么还是有错啊????
那位哥哥能帮我解决啊
{
//this.rows=row;
//this.cols=col;
Matrix A=new Matrix();
A.data[row][col]=value;
return A;
}
将函数改成现在这个形式,
为什么还是有错啊????
那位哥哥能帮我解决啊
#5
妹子,你这代码有很多错 啊...
#6
class Matrix {
int rows;
int cols;
double data[][];
Matrix() {
rows = 50;
cols = 50;
data = new double[rows][cols];
}
Matrix(int rows, int cols) {
this.rows = rows;
this.cols = cols;
data = new double[this.rows][this.cols];
}
Matrix(double data[][]) {
this.rows = data.length;
this.cols = data[0].length;
this.data = data;
}
Matrix(int rows, int cols, double data[][]) {
if(rows < data.length || cols < data[0].length){
System.out.println("Matrix(int rows, int cols, double data[][]) Error!");
return;
}
this.rows = rows;
this.cols = cols;
this.data = new Matrix(rows, cols).getData();
for (int i = 0; i < rows; i++){
for (int j = 0; j < cols; j++) {
this.data[i][j] = data[i][j];
}
}
}
boolean checkError(int row,int col,double [][] data){
if(row > data.length || col > data[0].length){
System.out.println("checkError Error!");
return false;
}
return true;
}
double getData(int row, int col) {
if(!checkError(row, col, this.data)){
return 0;
}
return data[row][col];
}
double[][] getData() {
return data;
}
void setData(int row, int col, double value) {
if(!checkError(row, col, this.data)){
return;
}
this.data[row][col] = value;
}
double[][] multiply(Matrix m) {
Matrix n = new Matrix(this.rows,this.cols);
for (int i = 0; i < m.rows; i++) {
for (int j = 0; j < this.cols; j++) {
n.data[i][j] = 0;
for (int k = 0; k < m.cols; k++)
n.data[i][j] += m.data[i][k] * this.data[k][j];
}
}
return n.data;
}
}
class MatrixTest{
public static void print(double[][] n) {
print("", n);
}
public static void print(String name,double[][] n) {
System.out.println("---------------------------" + name + "---------------------------");
for (int i = 0; i < n.length; i++){
double[] l = n[i];
for (int j = 0; j < l.length; j++) {
System.out.print(n[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
Matrix x = new Matrix(2, 3);
for (int i = 0; i < 2; i++){
for (int j = 0; j < 3; j++) {
double value = (i + 1) * (j + 2);
x.setData(i, j, value);
}
}
double b[][] = { { 1, 5, 2, 8 }, { 5, 9, 10, -3 }, { 2, 7, -5, -18 } };
Matrix y = new Matrix(3, 4, b);
print(" x ", x.data);
print(" y ", y.data);
print(" result ",y.multiply(x));
}
}
int rows;
int cols;
double data[][];
Matrix() {
rows = 50;
cols = 50;
data = new double[rows][cols];
}
Matrix(int rows, int cols) {
this.rows = rows;
this.cols = cols;
data = new double[this.rows][this.cols];
}
Matrix(double data[][]) {
this.rows = data.length;
this.cols = data[0].length;
this.data = data;
}
Matrix(int rows, int cols, double data[][]) {
if(rows < data.length || cols < data[0].length){
System.out.println("Matrix(int rows, int cols, double data[][]) Error!");
return;
}
this.rows = rows;
this.cols = cols;
this.data = new Matrix(rows, cols).getData();
for (int i = 0; i < rows; i++){
for (int j = 0; j < cols; j++) {
this.data[i][j] = data[i][j];
}
}
}
boolean checkError(int row,int col,double [][] data){
if(row > data.length || col > data[0].length){
System.out.println("checkError Error!");
return false;
}
return true;
}
double getData(int row, int col) {
if(!checkError(row, col, this.data)){
return 0;
}
return data[row][col];
}
double[][] getData() {
return data;
}
void setData(int row, int col, double value) {
if(!checkError(row, col, this.data)){
return;
}
this.data[row][col] = value;
}
double[][] multiply(Matrix m) {
Matrix n = new Matrix(this.rows,this.cols);
for (int i = 0; i < m.rows; i++) {
for (int j = 0; j < this.cols; j++) {
n.data[i][j] = 0;
for (int k = 0; k < m.cols; k++)
n.data[i][j] += m.data[i][k] * this.data[k][j];
}
}
return n.data;
}
}
class MatrixTest{
public static void print(double[][] n) {
print("", n);
}
public static void print(String name,double[][] n) {
System.out.println("---------------------------" + name + "---------------------------");
for (int i = 0; i < n.length; i++){
double[] l = n[i];
for (int j = 0; j < l.length; j++) {
System.out.print(n[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
Matrix x = new Matrix(2, 3);
for (int i = 0; i < 2; i++){
for (int j = 0; j < 3; j++) {
double value = (i + 1) * (j + 2);
x.setData(i, j, value);
}
}
double b[][] = { { 1, 5, 2, 8 }, { 5, 9, 10, -3 }, { 2, 7, -5, -18 } };
Matrix y = new Matrix(3, 4, b);
print(" x ", x.data);
print(" y ", y.data);
print(" result ",y.multiply(x));
}
}
#7
multiply核心算法没有改你的,你自己改吧
#8
class Matrix {
int rows;
int cols;
double data[][];
public Matrix() {
}
public Matrix(int rows, int cols) {
this.rows = rows;
this.cols = cols;
data = new double[rows][cols];
}
public Matrix(int rows, int cols, double data[][]) {
this.rows = rows;
this.cols = cols;
this.data = data;
}
double getData(int row, int col) {
return data[row][col];
}
void setData(int row, int col, double value) {
this.data[row][col] = value;
}
public double[][] multiply(Matrix m) {
Matrix n = new Matrix();
n.data = new double[m.rows][this.cols];
for (int i = 0; i < m.rows; i++) {
for (int j = 0; j < this.cols; j++) {
n.data[i][j] = 0;
for (int k = 0; k < m.cols; k++)
n.data[i][j] += m.data[i][k] * this.data[k][j];
}
}
return n.data;
}
void print(Matrix n) {
for (int i = 0; i < n.rows; i++) {
for (int j = 0; j < n.cols; j++) {
System.out.print(n.data[i][j] + "\t");
}
System.out.println();
}
}
}
public class MatrixTest
{
public static void main(String[] args) {
Matrix x = new Matrix(3, 3);
for (int i = 0; i < x.rows; i++)
for (int j = 0; j < x.cols; j++) {
x.setData(i, j, (i + 1) * (j + 2));
}
double b[][] = { { 1, 5, 2, 8 }, { 5, 9, 10, -3 }, { 2, 7, -5, -18 } };
Matrix y = new Matrix(b.length, b[0].length, b);
double d[][] = y.multiply(x);
Matrix k = new Matrix(d.length, d[0].length, d);
y.print(k);
}
}
这个可以运行,希望能帮到你!
#9
谢谢各位哥哥,真的帮了很大忙!!!
#1
我们下个星期就要交,为什么没有高手来帮我解决啊?????
#2
好像不可以通过形参来给定义数组长度。在定义的时候应该指定
#3
void setData(int row,int col,double value)
{
this.rows=row;
this.cols=col;
data=new double[this.rows][this.cols];
this.data[this.rows][this.cols]=value;
}
那这样写为什么还不对啊???
{
this.rows=row;
this.cols=col;
data=new double[this.rows][this.cols];
this.data[this.rows][this.cols]=value;
}
那这样写为什么还不对啊???
#4
Matrix setData(int row,int col,double value)
{
//this.rows=row;
//this.cols=col;
Matrix A=new Matrix();
A.data[row][col]=value;
return A;
}
将函数改成现在这个形式,
为什么还是有错啊????
那位哥哥能帮我解决啊
{
//this.rows=row;
//this.cols=col;
Matrix A=new Matrix();
A.data[row][col]=value;
return A;
}
将函数改成现在这个形式,
为什么还是有错啊????
那位哥哥能帮我解决啊
#5
妹子,你这代码有很多错 啊...
#6
class Matrix {
int rows;
int cols;
double data[][];
Matrix() {
rows = 50;
cols = 50;
data = new double[rows][cols];
}
Matrix(int rows, int cols) {
this.rows = rows;
this.cols = cols;
data = new double[this.rows][this.cols];
}
Matrix(double data[][]) {
this.rows = data.length;
this.cols = data[0].length;
this.data = data;
}
Matrix(int rows, int cols, double data[][]) {
if(rows < data.length || cols < data[0].length){
System.out.println("Matrix(int rows, int cols, double data[][]) Error!");
return;
}
this.rows = rows;
this.cols = cols;
this.data = new Matrix(rows, cols).getData();
for (int i = 0; i < rows; i++){
for (int j = 0; j < cols; j++) {
this.data[i][j] = data[i][j];
}
}
}
boolean checkError(int row,int col,double [][] data){
if(row > data.length || col > data[0].length){
System.out.println("checkError Error!");
return false;
}
return true;
}
double getData(int row, int col) {
if(!checkError(row, col, this.data)){
return 0;
}
return data[row][col];
}
double[][] getData() {
return data;
}
void setData(int row, int col, double value) {
if(!checkError(row, col, this.data)){
return;
}
this.data[row][col] = value;
}
double[][] multiply(Matrix m) {
Matrix n = new Matrix(this.rows,this.cols);
for (int i = 0; i < m.rows; i++) {
for (int j = 0; j < this.cols; j++) {
n.data[i][j] = 0;
for (int k = 0; k < m.cols; k++)
n.data[i][j] += m.data[i][k] * this.data[k][j];
}
}
return n.data;
}
}
class MatrixTest{
public static void print(double[][] n) {
print("", n);
}
public static void print(String name,double[][] n) {
System.out.println("---------------------------" + name + "---------------------------");
for (int i = 0; i < n.length; i++){
double[] l = n[i];
for (int j = 0; j < l.length; j++) {
System.out.print(n[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
Matrix x = new Matrix(2, 3);
for (int i = 0; i < 2; i++){
for (int j = 0; j < 3; j++) {
double value = (i + 1) * (j + 2);
x.setData(i, j, value);
}
}
double b[][] = { { 1, 5, 2, 8 }, { 5, 9, 10, -3 }, { 2, 7, -5, -18 } };
Matrix y = new Matrix(3, 4, b);
print(" x ", x.data);
print(" y ", y.data);
print(" result ",y.multiply(x));
}
}
int rows;
int cols;
double data[][];
Matrix() {
rows = 50;
cols = 50;
data = new double[rows][cols];
}
Matrix(int rows, int cols) {
this.rows = rows;
this.cols = cols;
data = new double[this.rows][this.cols];
}
Matrix(double data[][]) {
this.rows = data.length;
this.cols = data[0].length;
this.data = data;
}
Matrix(int rows, int cols, double data[][]) {
if(rows < data.length || cols < data[0].length){
System.out.println("Matrix(int rows, int cols, double data[][]) Error!");
return;
}
this.rows = rows;
this.cols = cols;
this.data = new Matrix(rows, cols).getData();
for (int i = 0; i < rows; i++){
for (int j = 0; j < cols; j++) {
this.data[i][j] = data[i][j];
}
}
}
boolean checkError(int row,int col,double [][] data){
if(row > data.length || col > data[0].length){
System.out.println("checkError Error!");
return false;
}
return true;
}
double getData(int row, int col) {
if(!checkError(row, col, this.data)){
return 0;
}
return data[row][col];
}
double[][] getData() {
return data;
}
void setData(int row, int col, double value) {
if(!checkError(row, col, this.data)){
return;
}
this.data[row][col] = value;
}
double[][] multiply(Matrix m) {
Matrix n = new Matrix(this.rows,this.cols);
for (int i = 0; i < m.rows; i++) {
for (int j = 0; j < this.cols; j++) {
n.data[i][j] = 0;
for (int k = 0; k < m.cols; k++)
n.data[i][j] += m.data[i][k] * this.data[k][j];
}
}
return n.data;
}
}
class MatrixTest{
public static void print(double[][] n) {
print("", n);
}
public static void print(String name,double[][] n) {
System.out.println("---------------------------" + name + "---------------------------");
for (int i = 0; i < n.length; i++){
double[] l = n[i];
for (int j = 0; j < l.length; j++) {
System.out.print(n[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
Matrix x = new Matrix(2, 3);
for (int i = 0; i < 2; i++){
for (int j = 0; j < 3; j++) {
double value = (i + 1) * (j + 2);
x.setData(i, j, value);
}
}
double b[][] = { { 1, 5, 2, 8 }, { 5, 9, 10, -3 }, { 2, 7, -5, -18 } };
Matrix y = new Matrix(3, 4, b);
print(" x ", x.data);
print(" y ", y.data);
print(" result ",y.multiply(x));
}
}
#7
multiply核心算法没有改你的,你自己改吧
#8
class Matrix {
int rows;
int cols;
double data[][];
public Matrix() {
}
public Matrix(int rows, int cols) {
this.rows = rows;
this.cols = cols;
data = new double[rows][cols];
}
public Matrix(int rows, int cols, double data[][]) {
this.rows = rows;
this.cols = cols;
this.data = data;
}
double getData(int row, int col) {
return data[row][col];
}
void setData(int row, int col, double value) {
this.data[row][col] = value;
}
public double[][] multiply(Matrix m) {
Matrix n = new Matrix();
n.data = new double[m.rows][this.cols];
for (int i = 0; i < m.rows; i++) {
for (int j = 0; j < this.cols; j++) {
n.data[i][j] = 0;
for (int k = 0; k < m.cols; k++)
n.data[i][j] += m.data[i][k] * this.data[k][j];
}
}
return n.data;
}
void print(Matrix n) {
for (int i = 0; i < n.rows; i++) {
for (int j = 0; j < n.cols; j++) {
System.out.print(n.data[i][j] + "\t");
}
System.out.println();
}
}
}
public class MatrixTest
{
public static void main(String[] args) {
Matrix x = new Matrix(3, 3);
for (int i = 0; i < x.rows; i++)
for (int j = 0; j < x.cols; j++) {
x.setData(i, j, (i + 1) * (j + 2));
}
double b[][] = { { 1, 5, 2, 8 }, { 5, 9, 10, -3 }, { 2, 7, -5, -18 } };
Matrix y = new Matrix(b.length, b[0].length, b);
double d[][] = y.multiply(x);
Matrix k = new Matrix(d.length, d[0].length, d);
y.print(k);
}
}
这个可以运行,希望能帮到你!
#9
谢谢各位哥哥,真的帮了很大忙!!!