Java Array to String arrayToString(String[] array)

Here you can find the source of arrayToString(String[] array)

Description

array To String

License

Apache License

Declaration

public static String arrayToString(String[] array) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Main {
    protected static final NumberFormat formatter = new DecimalFormat("###.###");

    public static String arrayToString(String[] array) {
        if (array.length == 0) {
            return "[]";
        }/*  w w  w  .  ja  v  a 2s .com*/
        StringBuilder str = new StringBuilder();
        str.append("[").append(array[0]);
        for (int i = 1; i < array.length; i++) {
            str.append(" ").append(array[i]);
        }
        str.append("]");
        return str.toString();
    }

    public static String arrayToString(double[] array) {
        if (array.length == 0) {
            return "[]";
        }
        StringBuilder str = new StringBuilder();
        str.append("[").append(formatDouble(array[0]));
        for (int i = 1; i < array.length; i++) {
            str.append(", ").append(formatDouble(array[i]));
        }
        str.append("]");
        return str.toString();
    }

    public static String arrayToString(float[] array) {
        if (array.length == 0) {
            return "[]";
        }
        StringBuilder str = new StringBuilder();
        str.append("[").append(formatDouble(array[0]));
        for (int i = 1; i < array.length; i++) {
            str.append(", ").append(formatDouble(array[i]));
        }
        str.append("]");
        return str.toString();
    }

    public static String arrayToString(int[] array) {
        if (array.length == 0) {
            return "[]";
        }
        StringBuilder str = new StringBuilder();
        str.append("[").append(formatDouble(array[0]));
        for (int i = 1; i < array.length; i++) {
            str.append(", ").append(formatDouble(array[i]));
        }
        str.append("]");
        return str.toString();
    }

    public static String formatDouble(double value) {
        return formatter.format(value);
    }

    public static String formatDouble(double value, int n) {
        StringBuilder str = new StringBuilder("###.");
        for (int nn = 0; nn < n; nn++) {
            str.append("#");
        }
        NumberFormat newFormatter = new DecimalFormat(str.toString());
        return newFormatter.format(value);
    }
}

Related

  1. arrayToString(String[] arr, String expr)
  2. arrayToString(String[] array)
  3. arrayToString(String[] array)
  4. arrayToString(String[] array)
  5. arrayToString(String[] array)
  6. arrayToString(String[] array)
  7. arrayToString(String[] array)
  8. arrayToString(String[] array)
  9. arrayToString(String[] array)