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

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

Description

to String

License

Open Source License

Parameter

Parameter Description
array a parameter

Return

String

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Manchester Centre for Integrative Systems Biology
 * University of Manchester/*from   w ww.  j  av  a 2  s.c  o m*/
 * Manchester M1 7ND
 * United Kingdom
 * 
 * Copyright (C) 2007 University of Manchester
 * 
 * This program is released under the Academic Free License ("AFL") v3.0.
 * (http://www.opensource.org/licenses/academic.php)
 *******************************************************************************/

import java.util.*;

public class Main {
    /**
     * 
     */
    public static final String ELEMENT_SEPARATOR = ",(\\s)*";
    /**
     * 
     */
    private static final String EMPTY_STRING = "";
    /**
     * 
     */
    private static final String ARRAY_SEPARATOR = ";";
    /**
     * 
     */
    private static final String ARRAY_OPEN_TAG = "[";
    /**
     * 
     */
    private static final String ARRAY_CLOSE_TAG = "]";

    /**
     * 
     * @param array
     * @return String
     */
    public static String toString(double[][] array) {
        final StringBuffer buffer = new StringBuffer(ARRAY_OPEN_TAG);

        for (int i = 0; i < array.length; i++) {
            final StringBuffer tempBuffer = new StringBuffer(Arrays.toString(array[i]));
            buffer.append(tempBuffer.substring(1, tempBuffer.length() - 1));
            buffer.append(ARRAY_SEPARATOR);
        }

        buffer.append(ARRAY_CLOSE_TAG);
        return buffer.toString();
    }

    /**
     * 
     * @param collection
     * @return String
     */
    public static String toString(final Collection<?> collection) {
        return toString(collection.toArray());
    }

    /**
     * 
     * @param array
     * @return String
     */
    public static String toString(final Object[] array) {
        return Arrays.toString(array).replace(ARRAY_OPEN_TAG, EMPTY_STRING).replace(ARRAY_CLOSE_TAG, EMPTY_STRING);
    }

    /**
     * 
     * @param value
     * @return Object[]
     */
    public static Object[] toArray(final String value) {
        if (value == null) {
            return new Object[] {};
        }

        final int BRACKET_LENGTH = 1;
        final String strippedValue = value.substring(BRACKET_LENGTH, value.length() - BRACKET_LENGTH);
        final StringTokenizer tokenizer = new StringTokenizer(strippedValue, ELEMENT_SEPARATOR);
        final Collection<Object> collection = new ArrayList<>();

        while (tokenizer.hasMoreTokens()) {
            collection.add(tokenizer.nextToken().trim());
        }

        return collection.toArray();
    }
}

Related

  1. matrixToString(float[][][] matrix)
  2. matrixToString(int[][] m)
  3. matrixToString(int[][] matrix, String sep)
  4. matrixToString(String[][] m)
  5. toString(double[][] A)
  6. toString(double[][] data)
  7. toString(int partition[][])
  8. toString(int[][] a)
  9. toString(int[][] matrix)