Concatenates all the passed arrays : Array Insert Remove « Collections Data Structure « Java






Concatenates all the passed arrays

      

/*
 * 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;
    }
}

   
    
    
    
    
    
  








Related examples in the same category

1.Adds all the elements of the given arrays into a new boolean-value array.
2.Adds all the elements of the given arrays into a new byte-type array.
3.Adds all the elements of the given arrays into a new char-type array.
4.Adds all the elements of the given arrays into a new double-type array.
5.Adds all the elements of the given arrays into a new float-type array.
6.Adds all the elements of the given arrays into a new int-type array.
7.Adds all the elements of the given arrays into a new long-type array.
8.Adds all the elements of the given arrays into a new short-type array.
9.Copies the given array and adds the given element at the end of the new array. (char type value)
10.Copies the given array and adds the given element at the end of the new array. (float type value)
11.Copies the given array and adds the given element at the end of the new array. (long value type)
12.Copies the given array and adds the given element at the end of the new array. (object value type)
13.Copies the given array and adds the given element at the end of the new array.(boolean value type)
14.Copies the given array and adds the given element at the end of the new array.(byte value type)
15.Copies the given array and adds the given element at the end of the new array.(double type value)
16.Copies the given array and adds the given element at the end of the new array.(int value type)
17.Copies the given array and adds the given element at the end of the new array.(short type array)
18.Inserts the specified element at the specified position in the array.
19.Inserts the specified element at the specified position in the boolean-type-value array.
20.Inserts the specified element at the specified position in the byte-type-value array.
21.Inserts the specified element at the specified position in the char-type-value array.
22.Inserts the specified element at the specified position in the double-type-value array.
23.Inserts the specified element at the specified position in the float-value-type array.
24.Inserts the specified element at the specified position in the int-type-value array.
25.Inserts the specified element at the specified position in the long-type-value array.
26.Inserts the specified element at the specified position in the short-value-type array.
27.Removes the element at the specified position from the specified array.
28.Removes the element at the specified position from the specified long type array.
29.Removes the first occurrence of the specified element from the specified array.
30.Removes the first occurrence of the specified element from the specified long value array.
31.Insert value to array
32.Array Copy Utilities
33.Concatenate Java arrays
34.This program demonstrates array manipulation.
35.Appends an Object to an Object array.
36.Appends an integer to an integer array.
37.Inserts an Object into an Object array at the index position.
38.Prepends an Object to an Object array.
39.Removes an Object from an Object array.
40.Removes duplicate elements from the array
41.Creates a new subarray from a larger array.
42.Append one array to another
43.Array Expander
44.Array Util: seach, insert, append, remove, copy, shuffle
45.Add new value to exsisting array.The new value is indexed to the last.
46.Add new value, which sets as the first value, to existing array.
47.Add one array to another
48.Returns a copy of the specified array of objects of the specified size.
49.Pack chunks
50.Removes an element from a an array yielding a new array with that data.