java题目,有没有大神愿意帮帮忙,谢谢~
1个回答
展开全部
public class Array {
private int row;//行
private int col;//列
private int[][] value = new int[row][col];
public Array(int row, int col, int[][] value) {
this.row = row;
this.col = col;
this.value = value;
}
/**
* 加法
* @param param
* @return
*/
public Array add(Array param) {
int[][] result = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
result[i][j] = value[i][j] + param.value[i][j];
}
}
return new Array(row, col, result);
}
/**
* 减法
* @param param
* @return
*/
public Array sub(Array param) {
int[][] result = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
result[i][j] = value[i][j] - param.value[i][j];
}
}
return new Array(row, col, result);
}
public static void main(String[] args) {
int[][] value1 = new int[][]{{1, 2}, {3, 4}, {5, 6}};
int[][] value2 = new int[][]{{7, 8}, {9, 10}, {11, 12}};
Array array1 = new Array(3, 2, value1);
Array array2 = new Array(3, 2, value2);
System.out.println("加法测试");
Array addResult = array1.add(array2);
for (int i = 0; i < addResult.row; i++) {
for (int j = 0; j < addResult.col; j++) {
System.out.print(addResult.value[i][j] + " ");
}
System.out.println();
}
System.out.println("减法测试");
Array subResult = array1.sub(array2);
for (int i = 0; i < subResult.row; i++) {
for (int j = 0; j < subResult.col; j++) {
System.out.print(subResult.value[i][j] + " ");
}
System.out.println();
}
}
}
结果:
加法测试
8 10
12 14
16 18
减法测试
-6 -6
-6 -6
-6 -6
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询