Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.lang.reflect.Array;

public class Main {
    /**
     * 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;
    }
}