This utility function builds an printable string of the elements (values) of a given 2D double array and returns it. - Java java.lang

Java examples for java.lang:String Array

Description

This utility function builds an printable string of the elements (values) of a given 2D double array and returns it.

Demo Code


import java.util.Arrays;

public class Main{
    /**/*  w  w w.  ja va2 s  .  co m*/
     * This utility function builds an printable string of the elements (values)
     * of a given 2D double array and returns it. The string could be used in
     * any standard print/output function (such as println)
     * 
     * @param a
     *            2D doubleArray to be printed
     * @return a ready-to-be-printed string of array elements/values
     */
    public static String toString(double[][] twoDdoubleArray) {
        String output = "";
        for (int row = 0; row < twoDdoubleArray.length; row++) {
            double[] aRowArray = ArraysUtils.rowCopy(twoDdoubleArray, row);
            output += "r" + row + Arrays.toString(aRowArray);
            output += "\n";
        }
        return output;
    }
    /**
     * This utility function returns an array copy containing of passed row
     * index of a given 2D array
     * 
     * @param twoDdoubleArray
     *            the original 2D double array from which a row will be copied
     *            and return as an array
     * @param row
     *            a row index indicating the row which needs to be taken out
     *            (copied and returned)
     * @return an array (double) containing a copy of its needed row (index
     *         passed as argument)
     */
    public static double[] rowCopy(double[][] twoDdoubleArray, int row) {
        double[] rowCopyArr = new double[twoDdoubleArray[0].length];
        for (int col = 0; col < twoDdoubleArray[0].length; col++) {
            rowCopyArr[col] = twoDdoubleArray[row][col];
        }
        return rowCopyArr;
    }
}

Related Tutorials