Java Array to String asString(int[] objects, String separator)

Here you can find the source of asString(int[] objects, String separator)

Description

as String

License

Open Source License

Declaration

public static String asString(int[] objects, String separator) 

Method Source Code

//package com.java2s;
/* Copyright (C) 2000-2003 The Software Conservancy as Trustee.
 * All rights reserved./*from w  w w  .  jav a  2s .  com*/
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Nothing in this notice shall be deemed to grant any rights to trademarks,
 * copyrights, patents, trade secrets or any other intellectual property of the
 * licensor or any contributor except as expressly stated herein. No patent
 * license is granted separate from the Software, for code that you delete from
 * the Software, or for combinations of the Software with other software or
 * hardware.
 */

import java.util.*;

public class Main {
    public static String asString(int[] objects, String separator) {
        if (objects == null)
            return "";

        StringBuffer buffer = new StringBuffer(objects.length * 10);
        int j = 0;
        for (int i = 0; i < objects.length; i++) {
            if (j > 0)
                buffer.append(separator);
            buffer.append(objects[i]);
            j++;
        }
        return buffer.toString();
    }

    public static String asString(Object[] objects, String separator) {
        if (objects == null)
            return "";

        StringBuffer buffer = new StringBuffer(objects.length * 10);
        int j = 0;
        for (int i = 0; i < objects.length; i++) {
            if (objects[i] != null) {
                if (j > 0)
                    buffer.append(separator);
                buffer.append(objects[i]);
                j++;
            }
        }
        return buffer.toString();
    }

    public static String asString(Collection objects, String separator) {
        if (objects == null)
            return "";

        StringBuffer buffer = new StringBuffer(objects.size() * 10);
        int i = 0;
        for (Iterator iterator = objects.iterator(); iterator.hasNext();) {
            Object object = iterator.next();
            if (object != null) {
                if (i > 0)
                    buffer.append(separator);
                buffer.append(object);
                i++;
            }
        }
        return buffer.toString();
    }

    public static String asString(Enumeration objects, String separator) {
        if (objects == null)
            return "";

        StringBuffer buffer = new StringBuffer();
        int i = 0;

        while (objects.hasMoreElements()) {
            Object object = objects.nextElement();
            if (object != null) {
                if (i > 0)
                    buffer.append(separator);
                buffer.append(object);
                i++;
            }
        }
        return buffer.toString();
    }
}

Related

  1. arrayToStringNullable(String[] a)
  2. arrayToStrings(String[] s, String delimiter)
  3. arrayToStringWithDifferenceOrientedFormat(double[] values, int minDigits)
  4. arrayToStringWithPrefix(T[] array, String splitter, String prefix)
  5. asString(final String[] array)
  6. asString(String[] arrayParameter)
  7. asString(T[] objects)
  8. asStringOn(StringBuilder sb, Object[] items, String separator)
  9. bitsToString(int[] arr)