Java Matrix to String Array2DToString(double[][] array)

Here you can find the source of Array2DToString(double[][] array)

Description

print 2D array to string (matrix)

License

Open Source License

Parameter

Parameter Description
array a parameter

Declaration

public static String Array2DToString(double[][] array) 

Method Source Code


//package com.java2s;
/*/*ww  w. j  a  va 2  s.  c o m*/
Copyright (c) 2014 cs.nmsu.edu
    
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
    
The Software shall be used for Good, not Evil.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

import java.util.*;

public class Main {
    /**
     * print 2D array to string (matrix)
     * @param array
     * @return
     */
    public static String Array2DToString(double[][] array) {
        final StringBuffer str = new StringBuffer();
        int i = 0, j = 0;
        for (i = 0; i < array.length; i++) {
            for (j = 0; j < array[i].length; j++) {
                str.append(" " + array[i][j]);
            }
            str.append("\n");
        }
        str.append("\n");

        return str.toString();
    }

    /**
     * Convert a 2D array to string
     * @param data
     * @return
     */
    public static String toString(double[][] data) {
        StringBuffer buf = new StringBuffer();

        //added by Huiping
        buf.append("row num = word num=" + data.length + "\n");
        if (data.length > 0)
            buf.append("col num = doc num = " + data[0].length + "\n");
        //end of adding

        for (int c0 = 0; c0 < data.length; c0++) {
            double[] row = data[c0];
            buf.append(Arrays.toString(row)).append("\n");
        }
        return buf.toString();
        //return Arrays.deepToString(data);
    }
}

Related

  1. array2DToString(int[][] grid)
  2. formatTable(String[][] table)
  3. matrix2string(int[][] matrix)
  4. matrix_toString(int[][] matrix)