Java Array Concatenate concat(int[] first, int[] second)

Here you can find the source of concat(int[] first, int[] second)

Description

concat

License

Open Source License

Declaration

public static int[] concat(int[] first, int[] second) 

Method Source Code


//package com.java2s;
/*//from  ww w .  j  a v a2s  . co  m
 * #%L
 * G
 * %%
 * Copyright (C) 2014 Giovanni Stilo
 * %%
 * G is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program.  If not, see
 * <https://www.gnu.org/licenses/lgpl-3.0.txt>.
 * #L%
 */

import java.util.Arrays;

public class Main {
    public static int[] concat(int[] first, int[] second) {
        if (first == null && second == null) {
            return new int[0];
        }

        if (first == null) {
            return Arrays.copyOf(second, second.length);
        }

        if (second == null) {
            return Arrays.copyOf(first, first.length);
        }

        int[] result = Arrays.copyOf(first, first.length + second.length);
        System.arraycopy(second, 0, result, first.length, second.length);
        return result;
    }

    public static long[] concat(long[] first, long[] second) {
        if (first == null && second == null) {
            return new long[0];
        }

        if (first == null) {
            return Arrays.copyOf(second, second.length);
        }

        if (second == null) {
            return Arrays.copyOf(first, first.length);
        }

        long[] result = Arrays.copyOf(first, first.length + second.length);
        System.arraycopy(second, 0, result, first.length, second.length);
        return result;
    }
}

Related

  1. concat(byte[]... arrays)
  2. concat(double[] first, double[] second)
  3. concat(final T[] elements, final T... elementsToAppend)
  4. concat(final T[] first, final T[] second)
  5. concat(float[] A, float[] B)
  6. concat(int[]... arrays)
  7. concat(Object[] array)
  8. concat(Object[] first, Object second)
  9. concat(String[] array, String separator)