Java List to String listToString(List list)

Here you can find the source of listToString(List list)

Description

list To String

License

Apache License

Declaration

public static String listToString(List<?> list) 

Method Source Code

//package com.java2s;
/*/*  ww w.ja va  2  s . com*/
 * Copyright 2014 Attila Szegedi, Daniel Dekany, Jonathan Revusky
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Collection;
import java.util.List;

public class Main {
    public static String listToString(List<?> list) {
        return collectionToString("", list);
    }

    private static String collectionToString(String prefix,
            Collection<?> list) {
        if (list == null)
            return "null";

        StringBuilder sb = new StringBuilder();
        sb.append(prefix);
        sb.append('[');
        boolean first = true;
        for (Object item : list) {
            if (!first) {
                sb.append(", ");
            } else {
                first = false;
            }
            sb.append(item instanceof Object[] ? arrayToString((Object[]) item)
                    : item);
        }
        sb.append(']');
        return sb.toString();
    }

    public static String arrayToString(double[] xs) {
        StringBuilder sb = new StringBuilder();

        sb.append('[');
        for (double x : xs) {
            if (sb.length() != 1)
                sb.append(", ");
            sb.append(x);
        }
        sb.append(']');

        return sb.toString();
    }

    public static String arrayToString(Object[] array) {
        if (array == null)
            return "null";

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (int i = 0; i < array.length; i++) {
            if (i != 0) {
                sb.append(", ");
            }
            sb.append(array[i]);
        }
        sb.append(']');
        return sb.toString();
    }

    public static String arrayToString(Object[][] arrayArray) {
        if (arrayArray == null)
            return "null";

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        boolean first = true;
        for (Object[] array : arrayArray) {
            if (!first) {
                sb.append(", ");
            } else {
                first = false;
            }
            sb.append(arrayToString(array));
        }
        sb.append(']');
        return sb.toString();
    }

    public static String arrayToString(int[] array) {
        if (array == null)
            return "null";

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (int i = 0; i < array.length; i++) {
            if (i != 0) {
                sb.append(", ");
            }
            sb.append(array[i]);
        }
        sb.append(']');
        return sb.toString();
    }

    public static String arrayToString(int[][] xss) {
        if (xss == null)
            return "null";

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (int i = 0; i < xss.length; i++) {
            if (i != 0) {
                sb.append(", ");
            }
            sb.append(arrayToString(xss[i]));
        }
        sb.append(']');
        return sb.toString();
    }

    public static String arrayToString(char[] array) {
        if (array == null)
            return "null";

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (int i = 0; i < array.length; i++) {
            if (i != 0) {
                sb.append(", ");
            }
            sb.append(array[i]);
        }
        sb.append(']');
        return sb.toString();
    }

    public static String arrayToString(boolean[] array) {
        if (array == null)
            return "null";

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (int i = 0; i < array.length; i++) {
            if (i != 0) {
                sb.append(", ");
            }
            sb.append(array[i]);
        }
        sb.append(']');
        return sb.toString();
    }
}

Related

  1. ListToString(List lista)
  2. listToString(List collection)
  3. listToString(List items)
  4. listToString(List l, String delim)
  5. listToString(List l, String sep)
  6. listToString(List list)
  7. listToString(List list, String separator, String prefix, String suffix)
  8. listToString(List list, String seperator)
  9. listToString(List list)