Java Array to String toString(Object[] array)

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

Description

to String

License

Apache License

Declaration

public static String toString(Object[] array) 

Method Source Code

//package com.java2s;
/**/*ww w . ja  v  a 2 s . co  m*/
 * Copyright ? 2013 - 2017 WaveMaker, Inc.
 *
 * 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.Iterator;

public class Main {
    public static String toString(Object[] array) {
        return toString(array, ", ");
    }

    public static String toString(Object[] array, String sep) {
        if (array == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            sb.append(String.valueOf(array[i]));
            if (i < array.length - 1) {
                sb.append(sep);
            }
        }
        return sb.toString();
    }

    public static <T> String toString(Collection<T> c) {
        return toString(c, ", ");
    }

    public static <T> String toString(Collection<T> c, String sep) {
        StringBuilder sb = new StringBuilder();
        for (Iterator<T> iter = c.iterator(); iter.hasNext();) {
            sb.append(String.valueOf(iter.next()));
            if (iter.hasNext()) {
                sb.append(sep);
            }
        }
        return sb.toString();
    }
}

Related

  1. toString(Object array)
  2. toString(Object array[])
  3. toString(Object[] a)
  4. toString(Object[] array)
  5. toString(Object[] array)
  6. toString(Object[] array)
  7. toString(Object[] objects)
  8. toString(String arrayName, T[] targets)
  9. toString(String[] args, char color1, char color2)