java如何将照片转换为矩阵_将多维Java数组转换为字符串矩阵?

我似乎无法让这个toString()方法工作?deepToString方法工作得很好,只是我必须有组织地打印出来,就像一个有对齐行和列的矩阵一样。我前一阵子已经开始工作了,但我改变了一些东西,现在天知道我做了什么,我不知道如何找回它。不管怎样,有人知道如何将多维数组输出为类似矩阵的字符串形式吗?

_

import java.util.Arrays;

public class MatrixOperations {

public static void main(String[] args) {

double[][] matrix1 = { { 0.0, 1.0, 2.0 }, { 3.0, 4.0, 5.0 },

{ 6.0, 7.0, 0.8 }, };

double[][] matrix2 = { { 1.0, 1.0, 1.0 }, { 0.0, 0.0, 0.0 },

{ 2.0, 2.0, 2.0 } };

System.out.println(toString(matrix1));

System.out.println(Arrays.deepToString(add(matrix1, matrix2)));

}

// Throws an IllegalArgumentException unless A and B contain n arrays of

// doubles, each of

// which contains m doubles, where both n and m are positive. (In other

// words, both A

// and B are n-by-m arrays.)

//

// Otherwise, returns the n-by-m array that represents the matrix sum of A

// and B.

public static double[][] add(double[][] A, double[][] B) {

if (A.length != B.length || A[1].length != B[1].length) {

throw new IllegalArgumentException("Rows and Columns Must Be Equal");

}

double[][] S = new double[A.length][A[1].length];

for (int i = 0; i < A.length; i++) {

// double valueAt = ;

for (int j = 0; j < A[1].length; j++) {

S[i][j] = A[i][j] + B[i][j];

}

}

return S;

}

// Throws an IllegalArgumentException unless A contains n arrays of doubles,

// each of

// which contains k doubles, and B contains k arrays of doubles, each of

// which contains

// m doubles, where n, k, and m are all positive. (In other words, A is an

// n-by-k array and B is a k-by-m array.)

// Otherwise, returns the n-by-m array that represents the matrix product of

// A and B.

// public static double[][] mul (double[][] A, double[][] B) {

// if (A[1].length != B.length){

// throw new IllegalArgumentException("Column-A Must Equal Row-B");

// }

// }

// Throws an IllegalArgumentException unless M contains n arrays of doubles,

// each of

// which contains m doubles, where both n and m are positive. (In other

// words, M

// is a n-by-m array.

// Otherwise, returns a String which, when printed, will be M displayed as a

// nicely

// formatted n-by-m table of doubles.

public static String toString(double[][] M) {

String separator = ", ";

StringBuffer result = new StringBuffer();

if (M.length > 0) {

result.append(M[0]);

for (int i = 0; i < M.length; i++) {

result.append(separator);

result.append(M[i]);

}

}

return result.toString();

}

}

谢谢你的帮助!:)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值