Java Array Join join(T[] array, String join)

Here you can find the source of join(T[] array, String join)

Description

join

License

Open Source License

Declaration

public static <T> String join(T[] array, String join) 

Method Source Code

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

import java.util.*;

public class Main {
    public static <T> String join(T[] array, String join) {
        return join(array, join, array.length);
    }//from   w  ww.j a va  2 s.c  om

    public static <T> String join(T[] array, String join, int limit) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < array.length && i < limit; i++) {
            builder.append(array[i].toString());

            int pos = i + 1;
            boolean last = pos < array.length && pos < limit;
            if (!last) {
                builder.append(join);
            }
        }

        return builder.toString();
    }

    public static <T> String join(List<T> list, String join) {
        return join(list, join, list.size());
    }

    public static <T> String join(List<T> list, String join, int limit) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < list.size() && i < limit; i++) {
            builder.append(list.get(i).toString());

            int pos = i + 1;
            boolean last = pos < list.size() && pos < limit;
            if (!last) {
                builder.append(join);
            }
        }

        return builder.toString();
    }
}

Related

  1. join(String[] x)
  2. join(String[]... arrays)
  3. join(String[]... values)
  4. join(T[] a, T[] b)
  5. join(T[] arr1, T[] arr2)
  6. join(T[] array, String separator)
  7. join(T[] array, String separator)
  8. join(T[] array, String separator)
  9. join(T[] array, String separator)