将多维Java数组转换为String-Matrix?

时间:2010-11-08 00:12:27

标签: java string matrix multidimensional-array

我似乎无法让这个toString()方法起作用? deepToString方法非常有效,除了我必须以有条理的方式打印它们,就像矩阵看起来像对齐的行和列一样。我不久前工作了,但我改变了一些东西,现在上帝知道我做了什么,我不知道怎么回来。无论如何,有没有人知道如何将多维数组输出到像字符串形式的矩阵?

另外,我遇​​到的另一个问题是弄清楚如何检查数字> = 0,因为它们不能为负数。不知道怎么做?以为我可以存储每个值,因为它循环并检查它是否为负,但我只是一直感到困惑和/或遇到错误。关于这些问题的任何帮助将不胜感激,我一直在努力工作5个小时,并没有得到任何地方! _

这是我到目前为止的代码:

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();
}
}

感谢您的帮助! :)

2 个答案:

答案 0 :(得分:3)

您的toString方法不会打印第二个维度,您需要两个for循环:

public static String toString(double[][] M) {
    String separator = ", ";
    StringBuffer result = new StringBuffer();

    // iterate over the first dimension
    for (int i = 0; i < M.length; i++) {
        // iterate over the second dimension
        for(int j = 0; j < M[i].length; j++){
            result.append(M[i][j]);
            result.append(separator);
        }
        // remove the last separator
        result.setLength(result.length() - separator.length());
        // add a line break.
        result.append("\n");
    }
    return result.toString();
}

要检查给定矩阵中的所有数字都是非负数,您还需要迭代矩阵中的每个元素。如果任何数字为负数,您可以立即返回,如果您通过整个矩阵而没有发现任何负数,那么就没有:

public static boolean isNonNegative(double[][] m){
    // iterate over first dimension
    for(int i = 0; i < m.length; i++){
        // iterate over second dimension
        for(int j = 0; j < m[i].length; j++){
            if(m[i][j] < 0){
                // found a negative element, return immediately
                // since this means the whole matrix cannot be
                // called non-negative
                return false;
            }
        }
    }
    // if we get here then no elements have been found to be negative
    // thus the matrix is non-negative.
    return true;
}

旁注,在您add所在行的for (int j = 0; j < A[1].length; j++) {函数中,如果矩阵为1xN,则可能会出现问题。在Java中,数组是0索引的,第一行是0,而不是1。对您而言,一种通常更安全的方法是执行我在这两个代码示例中显示的内容,并根据您在A[i].length上编入索引的for循环确保使用i A行存在。

答案 1 :(得分:0)

Arrays.deepToString()怎么样?