Concatenate arrays

 
public class Main {
    /**
     * Returns the array of characters consisting of the members of
     * the first specified array followed by the specified character.
     *
     * @param cs Characters to start resulting array.
     * @param c Last character in resulting array.
     * @return Array of characters consisting of the characters in the
     * first array followed by the last character.
     * @throws NullPointerException If the array of characters is
     * null.
     */
    public static char[] concatenate(char[] cs, char c) {
        char[] result = new char[cs.length+1];
        for (int i = 0; i < cs.length; ++i)
            result[i] = cs[i];
        result[result.length-1] = c;
        return result;
    }



    /**
     * Returns a new array of strings containing the elements of the
     * first array of strings specified followed by the elements of
     * the second array of strings specified.
     *
     * @param xs First array of strings.
     * @param ys Second array of strings.
     * @return Concatenation of first array of strings followed by the
     * second array of strings.
     */
    public static String[] concatenate(String[] xs, String[] ys) {
        String[] result = new String[xs.length + ys.length];
        System.arraycopy(xs,0,result,0,xs.length);
        System.arraycopy(ys,0,result,xs.length,ys.length);
        return result;
    }




}
/*
 * Copyright WizTools.org
 * Licensed under the Apache License, Version 2.0:
 * http://www.apache.org/licenses/LICENSE-2.0
 */
//package org.wiztools.commons;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author subwiz
 */
public final class ArrayUtil {
    /**
     * Determines if the passed object is of type array.
     * @param o The object to determine if it is an array.
     * @return true if the passed object is an array.
     * @throws NullPointerException when the passed object is null.
     */
    public static boolean isArray(Object o) throws NullPointerException {
        if(o == null)
            throw new NullPointerException("Object is null: cannot determine if it is of array type.");
        else {
            return o.getClass().isArray();
        }
    }

    /**
     * Concatenates all the passed parameters.
     * @param <T>
     * @param objs
     * @return
     */
    public static <T> T[] concat(T[] ... objs){
        List<T> out = new ArrayList<T>();

        int i = 0;
        T[] pass = null;
        for(T[] o: objs){
            for(int j=0; j<o.length; j++){
                out.add(o[j]);
                i++;
            }
            pass = o;
        }

        return out.toArray(pass);
    }

    // Primitive types for quicker copy:

    public static short[] concat(short[] ... objs){
        int count = 0;
        for(short[] o: objs){
            count += o.length;
        }

        final short[] out = new short[count];

        int i = 0;
        for(short[] o: objs){
            for(int j=0; j<o.length; j++){
                out[i] = o[j];
                i++;
            }
        }

        return out;
    }

    public static int[] concat(int[] ... objs){
        int count = 0;
        for(int[] o: objs){
            count += o.length;
        }

        final int[] out = new int[count];

        int i = 0;
        for(int[] o: objs){
            for(int j=0; j<o.length; j++){
                out[i] = o[j];
                i++;
            }
        }

        return out;
    }

    public static long[] concat(long[] ... objs){
        int count = 0;
        for(long[] o: objs){
            count += o.length;
        }

        final long[] out = new long[count];

        int i = 0;
        for(long[] o: objs){
            for(int j=0; j<o.length; j++){
                out[i] = o[j];
                i++;
            }
        }

        return out;
    }

    public static byte[] concat(byte[] ... objs){
        int count = 0;
        for(byte[] o: objs){
            count += o.length;
        }

        final byte[] out = new byte[count];

        int i = 0;
        for(byte[] o: objs){
            for(int j=0; j<o.length; j++){
                out[i] = o[j];
                i++;
            }
        }

        return out;
    }

    public static char[] concat(char[] ... objs){
        int count = 0;
        for(char[] o: objs){
            count += o.length;
        }

        final char[] out = new char[count];

        int i = 0;
        for(char[] o: objs){
            for(int j=0; j<o.length; j++){
                out[i] = o[j];
                i++;
            }
        }

        return out;
    }

    public static float[] concat(float[] ... objs){
        int count = 0;
        for(float[] o: objs){
            count += o.length;
        }

        final float[] out = new float[count];

        int i = 0;
        for(float[] o: objs){
            for(int j=0; j<o.length; j++){
                out[i] = o[j];
                i++;
            }
        }

        return out;
    }

    public static double[] concat(double[] ... objs){
        int count = 0;
        for(double[] o: objs){
            count += o.length;
        }

        final double[] out = new double[count];

        int i = 0;
        for(double[] o: objs){
            for(int j=0; j<o.length; j++){
                out[i] = o[j];
                i++;
            }
        }

        return out;
    }

    public static boolean[] concat(boolean[] ... objs){
        int count = 0;
        for(boolean[] o: objs){
            count += o.length;
        }

        final boolean[] out = new boolean[count];

        int i = 0;
        for(boolean[] o: objs){
            for(int j=0; j<o.length; j++){
                out[i] = o[j];
                i++;
            }
        }

        return out;
    }
}
  
Home 
  Java Book 
    Runnable examples  

Data Type Array:
  1. Append item to array
  2. Append an object to an array.
  3. Append one array to another
  4. Clone Array
  5. Clones two dimensional float array
  6. Compare two arrays by reference
  7. Compare Two byte Arrays
  8. Compare Two boolean Arrays
  9. Compare Two char Arrays
  10. Compare Two double Arrays
  11. Compare Two float Arrays
  12. Compare Two int Arrays
  13. Compare Two long Arrays
  14. Compare two object arrays
  15. Compare Two short Arrays
  16. Compare two two-dimensional array
  17. Concatenate arrays
  18. Convert array to set
  19. Convert string array to string
  20. Convert string array to List
  21. Convert multi-dimensional array to string
  22. Convert an array to a Map
  23. Copy array
  24. Copy Elements from One Array to Another
  25. Copy and add an array at the end of the new array
  26. Extend an array with additional extra space
  27. Extend to Double the size of an array
  28. Fill boolean, byte, char,short, int, long, float array
  29. Get array upperbound
  30. Get the number of dimensions
  31. Insert an Element into a Sorted Array
  32. Insert the specified element at the specified position in the array
  33. Insert source array in the target array at offset
  34. Maximum value in an array
  35. Minimum value in an array.
  36. Merge two arrays
  37. Remove duplicate element from array
  38. Remove the element at the specified position from the specified array.
  39. Reverses the order of an array(byte, long, int)
  40. Search an element in an array and return the index and last index
  41. Binary Search on an Array
  42. Shift all elements right by one
  43. Shift all elements left by one
  44. Shuffle an array
  45. Sort byte(char,double, float, int) Array
  46. Sort char array
  47. Sort int Array
  48. Sort long Array
  49. Sort short Array
  50. Sort string type array in case insensitive order and case sensitive order
  51. Sort an Array in Descending (Reverse) Order
  52. Start with one array
  53. Subarray from array that starts at offset