Java Array Flatten flatten(byte[][] first)

Here you can find the source of flatten(byte[][] first)

Description

flatten

License

Open Source License

Declaration

public static byte[] flatten(byte[][] first) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Arrays;

public class Main {
    public static byte[] flatten(byte[][] first) {
        byte[] result = null;

        for (byte[] curr : first) {
            result = concat(result, curr);
        }/* w  w w.  j a  va2s. co m*/

        return result;
    }

    public static int[] flatten(int[][] first) {
        int[] result = null;

        for (int[] curr : first) {
            result = concat(result, curr);
        }

        return result;
    }

    public static long[] flatten(long[][] first) {
        long[] result = null;

        for (long[] curr : first) {
            result = concat(result, curr);
        }

        return result;
    }

    public static <T> T[] flatten(T[][] first) {
        T[] result = null;

        for (T[] curr : first) {
            result = concat(result, curr);
        }

        return result;
    }

    public static byte[] concat(byte[] first, byte[] second) {
        if (first == null) {
            if (second == null) {
                return null;
            }
            return second;
        }
        if (second == null) {
            return first;
        }

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

    public static int[] concat(int[] first, int[] second) {
        if (first == null) {
            if (second == null) {
                return null;
            }
            return second;
        }
        if (second == null) {
            return first;
        }

        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) {
            if (second == null) {
                return null;
            }
            return second;
        }
        if (second == null) {
            return first;
        }

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

    public static <T> T[] concat(T[] first, T[] second) {
        if (first == null) {
            if (second == null) {
                return null;
            }
            return second;
        }
        if (second == null) {
            return first;
        }

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

Related

  1. flatten(E[][] a)
  2. flatten(final Object[] array)
  3. flatten(float[][] mat)
  4. flatten(Object[] array)