Java Array Join join(Object[]... arrays)

Here you can find the source of join(Object[]... arrays)

Description

join

License

Open Source License

Parameter

Parameter Description
arrays a parameter

Return

the concatenation of the elements of the arrays into a new array, ignoring arrays that are null (but including null entries in each of the arrays)

Declaration

public static Object[] join(Object[]... arrays) 

Method Source Code

//package com.java2s;

import java.util.*;

public class Main {
    /**/*from  w  w w  .  ja  v a  2s .com*/
     * @param arrays
     * @return the concatenation of the elements of the arrays into a new array,
     * ignoring arrays that are null (but including null entries in each
     * of the arrays)
     */
    public static Object[] join(Object[]... arrays) {
        int size = totalSize(arrays);
        Object[] result = new Object[size];
        int i = 0;
        for (Object[] array : arrays) {
            if (array != null) {
                for (int j = 0; j < array.length; ++j, ++i) {
                    result[i] = array[j];
                }
            }
        }
        return result;
    }

    public static <T> String join(Collection<T> things, String delim) {
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (T t : things) {
            if (first) {
                first = false;
            } else {
                sb.append(delim);
            }
            sb.append(t);
        }
        return sb.toString();
    }

    /**
     * @param arrays
     * @return the sum of the lengths of the arrays, ignoring arrays that are
     * null (but counting null entries)
     */
    public static int totalSize(Object[]... arrays) {
        if (arrays == null) {
            return 0;
        }
        int size = 0;
        for (Object[] array : arrays) {
            size += (array == null ? 0 : array.length);
        }
        return size;
    }

    public static String toString(Object[] arr) {
        return toString(arr, true);
    }

    public static String toString(Object[] arr, boolean square) {
        if (arr == null) {
            return "null";
        }
        StringBuffer sb = new StringBuffer();
        if (square) {
            sb.append("[");
        } else {
            sb.append("(");
        }
        for (int i = 0; i < arr.length; ++i) {// Object o : arr ) {
            if (i > 0) {
                sb.append(",");
            }
            if (arr[i] == null) {
                sb.append("null");
            } else {
                sb.append(arr[i].toString());
            }
        }
        if (square) {
            sb.append("]");
        } else {
            sb.append(")");
        }

        return sb.toString();
    }
}

Related

  1. join(Object[] parts, String delim)
  2. join(Object[] strings, String delimiter)
  3. join(Object[] strings, String spliter)
  4. join(Object[] tokens, String delimiter)
  5. join(Object[] values, String join)
  6. join(String delimiter, Object[] strings)
  7. join(String delimiter, short[] array)
  8. join(String delimiter, String[] array)
  9. join(String delimiter, String[] elements)