Java tutorial
//package com.java2s; /* * Copyright (C) 2011 Francesco Feltrinelli <first_name DOT last_name AT gmail DOT com> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ import java.lang.reflect.Array; public class Main { public static byte[] concat(byte single, byte[] array) { byte[] result = new byte[1 + array.length]; result[0] = single; System.arraycopy(array, 0, result, 1, array.length); return result; } public static byte[] concat(byte[] array, byte single) { byte[] result = new byte[1 + array.length]; System.arraycopy(array, 0, result, 0, array.length); result[array.length] = single; return result; } public static byte[] concat(byte[] first, byte[] second) { byte[] result = new byte[first.length + second.length]; System.arraycopy(first, 0, result, 0, first.length); System.arraycopy(second, 0, result, first.length, second.length); return result; } public static byte[] concat(byte[] first, int firstStart, int firstLength, byte[] second, int secondStart, int secondLength) { byte[] result = new byte[firstLength + secondLength]; System.arraycopy(first, firstStart, result, 0, firstLength); System.arraycopy(second, secondStart, result, firstLength, secondLength); return result; } public static byte[] concat(byte[] first, byte[] second, int secondStart, int secondLength) { return concat(first, 0, first.length, second, secondStart, secondLength); } public static byte[] concat(byte[] first, int firstStart, int firstLength, byte[] second) { return concat(first, firstStart, firstLength, second, 0, second.length); } public static <T> T[] concat(T single, T[] array) { @SuppressWarnings("unchecked") final T[] result = (T[]) Array.newInstance(single.getClass().getComponentType(), 1 + array.length); result[0] = single; System.arraycopy(array, 0, result, 1, array.length); return result; } public static <T> T[] concat(T[] array, T single) { @SuppressWarnings("unchecked") final T[] result = (T[]) Array.newInstance(single.getClass().getComponentType(), 1 + array.length); System.arraycopy(array, 0, result, 0, array.length); result[array.length] = single; return result; } public static <T> T[] concat(T[] first, T[] second) { @SuppressWarnings("unchecked") final T[] result = (T[]) Array.newInstance(first.getClass().getComponentType(), first.length + second.length); System.arraycopy(first, 0, result, 0, first.length); System.arraycopy(second, 0, result, first.length, second.length); return result; } }