Java Array to String toString(String arrayName, T[] targets)

Here you can find the source of toString(String arrayName, T[] targets)

Description

Constructs a String representation of an array and its values.

License

Open Source License

Declaration

public static <T> String toString(String arrayName, T[] targets) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.List;

public class Main {
    /**/*from w ww  .j  av  a 2 s . com*/
     * Constructs a String representation of an array and its values.
     */
    public static <T> String toString(String arrayName, T[] targets) {
        final int n = targets.length;
        StringBuilder sb = new StringBuilder();

        // Generate value
        sb.append(arrayName).append(" { ");

        for (int i = 0; i < n; i++) {
            sb.append(targets[i].toString());

            if (i + 1 < n)
                sb.append(",");
            sb.append(" ");
        }
        return sb.append("}").toString();
    }

    /**
     * Constructs a String representation of an array and its values.
     */
    public static <T> String toString(T[] targets) {
        return toString(targets.getClass().getSimpleName(), targets);
    }

    /**
     * Constructs a String representation of a {@link List} and its values.
     */
    public static <T> String toString(List<T> values) {
        return toString(values.toArray());
    }

    /**
     * Constructs a String representation of a {@link List} and its values.
     */
    public static <T> String toString(String arrayName, List<T> values) {
        return toString(arrayName, values.toArray());
    }
}

Related

  1. toString(Object[] array)
  2. toString(Object[] array)
  3. toString(Object[] array)
  4. toString(Object[] array)
  5. toString(Object[] objects)
  6. toString(String[] args, char color1, char color2)
  7. toString(String[] arguments)
  8. toString(String[] array, String delimiter)
  9. toString(String[] line)