用java代码写一个程序可以计算矩阵的加减乘
时间: 2023-05-21 16:00:49 浏览: 73
非常好的问题!以下是一个简单的 Java 代码示例,可以计算矩阵的加减乘:
```java
public class Matrix {
private int[][] data;
private int rows;
private int cols;
public Matrix(int[][] data) {
this.data = data;
this.rows = data.length;
this.cols = data[0].length;
}
public Matrix add(Matrix other) {
if (this.rows != other.rows || this.cols != other.cols) {
throw new IllegalArgumentException("Matrix dimensions must match");
}
int[][] result = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = this.data[i][j] + other.data[i][j];
}
}
return new Matrix(result);
}
public Matrix subtract(Matrix other) {
if (this.rows != other.rows || this.cols != other.cols) {
throw new IllegalArgumentException("Matrix dimensions must match");
}
int[][] result = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = this.data[i][j] - other.data[i][j];
}
}
return new Matrix(result);
}
public Matrix multiply(Matrix other) {
if (this.cols != other.rows) {
throw new IllegalArgumentException("Matrix dimensions must match");
}
int[][] result = new int[this.rows][other.cols];
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < other.cols; j++) {
int sum = 0;
for (int k = 0; k < this.cols; k++) {
sum += this.data[i][k] * other.data[k][j];
}
result[i][j] = sum;
}
}
return new Matrix(result);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sb.append(data[i][j]).append(" ");
}
sb.append("\n");
}
return sb.toString();
}
}
```
使用示例:
```java
int[][] data1 = {{1, 2}, {3, 4}};
int[][] data2 = {{5, 6}, {7, 8}};
Matrix matrix1 = new Matrix(data1);
Matrix matrix2 = new Matrix(data2);
Matrix sum = matrix1.add(matrix2);
System.out.println("Sum:\n" + sum);
Matrix difference = matrix1.subtract(matrix2);
System.out.println("Difference:\n" + difference);
Matrix product = matrix1.multiply(matrix2);
System.out.println("Product:\n" + product);
```
输出结果:
```
Sum:
6 8
10 12
Difference:
-4 -4
-4 -4
Product:
19 22
43 50
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)