Transform two dimensional array as String - Java java.lang

Java examples for java.lang:String Array

Description

Transform two dimensional array as String

Demo Code


//package com.java2s;

public class Main {
    private static String newline = System.getProperty("line.separator");

    public static String asString(final Object[][] matrix) {
        final StringBuilder b = new StringBuilder();
        for (final Object[] o : matrix) {
            b.append(asString(o));//from   w  ww  . j av a  2 s .  c  o m
            b.append(newline);
        }
        return b.toString();
    }

    public static String asString(final Object[] vector) {
        final StringBuilder b = new StringBuilder();
        String retval = "null";
        if (vector == null) {
            retval = "null";
        } else {
            for (int i = 0; i < vector.length; i++) {
                if (i > 0) {
                    b.append(" ");
                }
                final Object obj = vector[i];
                b.append(obj == null ? "null" : "'" + obj.toString()
                        + "'\t");

            }
            retval = b.toString();
        }
        return retval;
    }
}

Related Tutorials