Java tutorial
//package com.java2s; import java.lang.reflect.Array; public class Main { /** * Inserts one array into another array. */ public static <T> T[] insert(T[] dest, T[] src, int offset) { Class<T> componentType = (Class<T>) dest.getClass().getComponentType(); return insert(dest, src, offset, componentType); } /** * Inserts one element into an array. */ public static <T> T[] insert(T[] dest, T src, int offset) { Class<T> componentType = (Class<T>) dest.getClass().getComponentType(); return insert(dest, src, offset, componentType); } /** * Inserts one array into another array. */ @SuppressWarnings({ "unchecked" }) public static <T> T[] insert(T[] dest, T[] src, int offset, Class componentType) { T[] temp = (T[]) Array.newInstance(componentType, dest.length + src.length); System.arraycopy(dest, 0, temp, 0, offset); System.arraycopy(src, 0, temp, offset, src.length); System.arraycopy(dest, offset, temp, src.length + offset, dest.length - offset); return temp; } /** * Inserts one element into another array. */ @SuppressWarnings({ "unchecked" }) public static <T> T[] insert(T[] dest, T src, int offset, Class componentType) { T[] temp = (T[]) Array.newInstance(componentType, dest.length + 1); System.arraycopy(dest, 0, temp, 0, offset); temp[offset] = src; System.arraycopy(dest, offset, temp, offset + 1, dest.length - offset); return temp; } /** * Inserts one array into another <code>String</code> array. */ public static String[] insert(String[] dest, String[] src, int offset) { String[] temp = new String[dest.length + src.length]; System.arraycopy(dest, 0, temp, 0, offset); System.arraycopy(src, 0, temp, offset, src.length); System.arraycopy(dest, offset, temp, src.length + offset, dest.length - offset); return temp; } /** * Inserts one element into another <code>String</code> array. */ public static String[] insert(String[] dest, String src, int offset) { String[] temp = new String[dest.length + 1]; System.arraycopy(dest, 0, temp, 0, offset); temp[offset] = src; System.arraycopy(dest, offset, temp, offset + 1, dest.length - offset); return temp; } /** * Inserts one array into another <code>byte</code> array. */ public static byte[] insert(byte[] dest, byte[] src, int offset) { byte[] temp = new byte[dest.length + src.length]; System.arraycopy(dest, 0, temp, 0, offset); System.arraycopy(src, 0, temp, offset, src.length); System.arraycopy(dest, offset, temp, src.length + offset, dest.length - offset); return temp; } /** * Inserts one element into another <code>byte</code> array. */ public static byte[] insert(byte[] dest, byte src, int offset) { byte[] temp = new byte[dest.length + 1]; System.arraycopy(dest, 0, temp, 0, offset); temp[offset] = src; System.arraycopy(dest, offset, temp, offset + 1, dest.length - offset); return temp; } /** * Inserts one array into another <code>char</code> array. */ public static char[] insert(char[] dest, char[] src, int offset) { char[] temp = new char[dest.length + src.length]; System.arraycopy(dest, 0, temp, 0, offset); System.arraycopy(src, 0, temp, offset, src.length); System.arraycopy(dest, offset, temp, src.length + offset, dest.length - offset); return temp; } /** * Inserts one element into another <code>char</code> array. */ public static char[] insert(char[] dest, char src, int offset) { char[] temp = new char[dest.length + 1]; System.arraycopy(dest, 0, temp, 0, offset); temp[offset] = src; System.arraycopy(dest, offset, temp, offset + 1, dest.length - offset); return temp; } /** * Inserts one array into another <code>short</code> array. */ public static short[] insert(short[] dest, short[] src, int offset) { short[] temp = new short[dest.length + src.length]; System.arraycopy(dest, 0, temp, 0, offset); System.arraycopy(src, 0, temp, offset, src.length); System.arraycopy(dest, offset, temp, src.length + offset, dest.length - offset); return temp; } /** * Inserts one element into another <code>short</code> array. */ public static short[] insert(short[] dest, short src, int offset) { short[] temp = new short[dest.length + 1]; System.arraycopy(dest, 0, temp, 0, offset); temp[offset] = src; System.arraycopy(dest, offset, temp, offset + 1, dest.length - offset); return temp; } /** * Inserts one array into another <code>int</code> array. */ public static int[] insert(int[] dest, int[] src, int offset) { int[] temp = new int[dest.length + src.length]; System.arraycopy(dest, 0, temp, 0, offset); System.arraycopy(src, 0, temp, offset, src.length); System.arraycopy(dest, offset, temp, src.length + offset, dest.length - offset); return temp; } /** * Inserts one element into another <code>int</code> array. */ public static int[] insert(int[] dest, int src, int offset) { int[] temp = new int[dest.length + 1]; System.arraycopy(dest, 0, temp, 0, offset); temp[offset] = src; System.arraycopy(dest, offset, temp, offset + 1, dest.length - offset); return temp; } /** * Inserts one array into another <code>long</code> array. */ public static long[] insert(long[] dest, long[] src, int offset) { long[] temp = new long[dest.length + src.length]; System.arraycopy(dest, 0, temp, 0, offset); System.arraycopy(src, 0, temp, offset, src.length); System.arraycopy(dest, offset, temp, src.length + offset, dest.length - offset); return temp; } /** * Inserts one element into another <code>long</code> array. */ public static long[] insert(long[] dest, long src, int offset) { long[] temp = new long[dest.length + 1]; System.arraycopy(dest, 0, temp, 0, offset); temp[offset] = src; System.arraycopy(dest, offset, temp, offset + 1, dest.length - offset); return temp; } /** * Inserts one array into another <code>float</code> array. */ public static float[] insert(float[] dest, float[] src, int offset) { float[] temp = new float[dest.length + src.length]; System.arraycopy(dest, 0, temp, 0, offset); System.arraycopy(src, 0, temp, offset, src.length); System.arraycopy(dest, offset, temp, src.length + offset, dest.length - offset); return temp; } /** * Inserts one element into another <code>float</code> array. */ public static float[] insert(float[] dest, float src, int offset) { float[] temp = new float[dest.length + 1]; System.arraycopy(dest, 0, temp, 0, offset); temp[offset] = src; System.arraycopy(dest, offset, temp, offset + 1, dest.length - offset); return temp; } /** * Inserts one array into another <code>double</code> array. */ public static double[] insert(double[] dest, double[] src, int offset) { double[] temp = new double[dest.length + src.length]; System.arraycopy(dest, 0, temp, 0, offset); System.arraycopy(src, 0, temp, offset, src.length); System.arraycopy(dest, offset, temp, src.length + offset, dest.length - offset); return temp; } /** * Inserts one element into another <code>double</code> array. */ public static double[] insert(double[] dest, double src, int offset) { double[] temp = new double[dest.length + 1]; System.arraycopy(dest, 0, temp, 0, offset); temp[offset] = src; System.arraycopy(dest, offset, temp, offset + 1, dest.length - offset); return temp; } /** * Inserts one array into another <code>boolean</code> array. */ public static boolean[] insert(boolean[] dest, boolean[] src, int offset) { boolean[] temp = new boolean[dest.length + src.length]; System.arraycopy(dest, 0, temp, 0, offset); System.arraycopy(src, 0, temp, offset, src.length); System.arraycopy(dest, offset, temp, src.length + offset, dest.length - offset); return temp; } /** * Inserts one element into another <code>boolean</code> array. */ public static boolean[] insert(boolean[] dest, boolean src, int offset) { boolean[] temp = new boolean[dest.length + 1]; System.arraycopy(dest, 0, temp, 0, offset); temp[offset] = src; System.arraycopy(dest, offset, temp, offset + 1, dest.length - offset); return temp; } }