Inserts one array into another at given offset. - Java java.lang

Java examples for java.lang:int Array

Description

Inserts one array into another at given offset.

Demo Code

// Copyright (c) 2003-present, Jodd Team (jodd.org). All Rights Reserved.
import java.lang.reflect.Array;
import static jodd.util.StringPool.NULL;

public class Main{
    /**//from   w  ww  .j a v  a 2s.com
     * Inserts one array into another at given offset.
     */
    public static <T> T[] insertAt(T[] dest, T[] src, int offset) {
        Class<T> componentType = (Class<T>) dest.getClass()
                .getComponentType();
        return insertAt(dest, src, offset, componentType);
    }
    /**
     * Inserts one array into another at given offset.
     */
    @SuppressWarnings({ "unchecked" })
    public static <T> T[] insertAt(T[] dest, T[] src, int offset,
            Class componentType) {
        T[] temp = (T[]) Array.newInstance(componentType, dest.length
                + src.length - 1);
        System.arraycopy(dest, 0, temp, 0, offset);
        System.arraycopy(src, 0, temp, offset, src.length);
        System.arraycopy(dest, offset + 1, temp, src.length + offset,
                dest.length - offset - 1);
        return temp;
    }
    /**
     * Inserts one array into another by replacing specified offset.
     */
    public static String[] insertAt(String[] dest, String[] src, int offset) {
        String[] temp = new String[dest.length + src.length - 1];
        System.arraycopy(dest, 0, temp, 0, offset);
        System.arraycopy(src, 0, temp, offset, src.length);
        System.arraycopy(dest, offset + 1, temp, src.length + offset,
                dest.length - offset - 1);
        return temp;
    }
    /**
     * Inserts one array into another by replacing specified offset.
     */
    public static byte[] insertAt(byte[] dest, byte[] src, int offset) {
        byte[] temp = new byte[dest.length + src.length - 1];
        System.arraycopy(dest, 0, temp, 0, offset);
        System.arraycopy(src, 0, temp, offset, src.length);
        System.arraycopy(dest, offset + 1, temp, src.length + offset,
                dest.length - offset - 1);
        return temp;
    }
    /**
     * Inserts one array into another by replacing specified offset.
     */
    public static char[] insertAt(char[] dest, char[] src, int offset) {
        char[] temp = new char[dest.length + src.length - 1];
        System.arraycopy(dest, 0, temp, 0, offset);
        System.arraycopy(src, 0, temp, offset, src.length);
        System.arraycopy(dest, offset + 1, temp, src.length + offset,
                dest.length - offset - 1);
        return temp;
    }
    /**
     * Inserts one array into another by replacing specified offset.
     */
    public static short[] insertAt(short[] dest, short[] src, int offset) {
        short[] temp = new short[dest.length + src.length - 1];
        System.arraycopy(dest, 0, temp, 0, offset);
        System.arraycopy(src, 0, temp, offset, src.length);
        System.arraycopy(dest, offset + 1, temp, src.length + offset,
                dest.length - offset - 1);
        return temp;
    }
    /**
     * Inserts one array into another by replacing specified offset.
     */
    public static int[] insertAt(int[] dest, int[] src, int offset) {
        int[] temp = new int[dest.length + src.length - 1];
        System.arraycopy(dest, 0, temp, 0, offset);
        System.arraycopy(src, 0, temp, offset, src.length);
        System.arraycopy(dest, offset + 1, temp, src.length + offset,
                dest.length - offset - 1);
        return temp;
    }
    /**
     * Inserts one array into another by replacing specified offset.
     */
    public static long[] insertAt(long[] dest, long[] src, int offset) {
        long[] temp = new long[dest.length + src.length - 1];
        System.arraycopy(dest, 0, temp, 0, offset);
        System.arraycopy(src, 0, temp, offset, src.length);
        System.arraycopy(dest, offset + 1, temp, src.length + offset,
                dest.length - offset - 1);
        return temp;
    }
    /**
     * Inserts one array into another by replacing specified offset.
     */
    public static float[] insertAt(float[] dest, float[] src, int offset) {
        float[] temp = new float[dest.length + src.length - 1];
        System.arraycopy(dest, 0, temp, 0, offset);
        System.arraycopy(src, 0, temp, offset, src.length);
        System.arraycopy(dest, offset + 1, temp, src.length + offset,
                dest.length - offset - 1);
        return temp;
    }
    /**
     * Inserts one array into another by replacing specified offset.
     */
    public static double[] insertAt(double[] dest, double[] src, int offset) {
        double[] temp = new double[dest.length + src.length - 1];
        System.arraycopy(dest, 0, temp, 0, offset);
        System.arraycopy(src, 0, temp, offset, src.length);
        System.arraycopy(dest, offset + 1, temp, src.length + offset,
                dest.length - offset - 1);
        return temp;
    }
    /**
     * Inserts one array into another by replacing specified offset.
     */
    public static boolean[] insertAt(boolean[] dest, boolean[] src,
            int offset) {
        boolean[] temp = new boolean[dest.length + src.length - 1];
        System.arraycopy(dest, 0, temp, 0, offset);
        System.arraycopy(src, 0, temp, offset, src.length);
        System.arraycopy(dest, offset + 1, temp, src.length + offset,
                dest.length - offset - 1);
        return temp;
    }
}

Related Tutorials