Java Matrix Flatten flatten(boolean[][] array)

Here you can find the source of flatten(boolean[][] array)

Description

Takes a two-dimensional array and creates a one dimensional array by placing the second dimension arrays one after another.

License

Apache License

Parameter

Parameter Description
array the array to flatten

Return

the flattened array

Declaration

public static boolean[] flatten(boolean[][] array) 

Method Source Code

//package com.java2s;
/*/*from   ww  w  .  j  a  v a 2  s .c om*/
 * Copyright 2012-2014 David Karnok
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Takes a two-dimensional array and creates a one dimensional
     * array by placing the second dimension arrays one after another.
     * @param array the array to flatten
     * @return the flattened array
     */
    public static boolean[] flatten(boolean[][] array) {
        int n = count(array);
        boolean[] result = new boolean[n];
        int o = 0;
        for (boolean[] b : array) {
            System.arraycopy(b, 0, result, o, b.length);
            o += b.length;
        }
        return result;
    }

    /**
     * Takes a two-dimensional array and creates a one dimensional
     * array by placing the second dimension arrays one after another.
     * @param array the array to flatten
     * @return the flattened array
     */
    public static byte[] flatten(byte[][] array) {
        int n = count(array);
        byte[] result = new byte[n];
        int o = 0;
        for (byte[] b : array) {
            System.arraycopy(b, 0, result, o, b.length);
            o += b.length;
        }
        return result;
    }

    /**
     * Takes a two-dimensional array and creates a one dimensional
     * array by placing the second dimension arrays one after another.
     * @param array the array to flatten
     * @return the flattened array
     */
    public static short[] flatten(short[][] array) {
        int n = count(array);
        short[] result = new short[n];
        int o = 0;
        for (short[] b : array) {
            System.arraycopy(b, 0, result, o, b.length);
            o += b.length;
        }
        return result;
    }

    /**
     * Takes a two-dimensional array and creates a one dimensional
     * array by placing the second dimension arrays one after another.
     * @param array the array to flatten
     * @return the flattened array
     */
    public static char[] flatten(char[][] array) {
        int n = count(array);
        char[] result = new char[n];
        int o = 0;
        for (char[] b : array) {
            System.arraycopy(b, 0, result, o, b.length);
            o += b.length;
        }
        return result;
    }

    /**
     * Takes a two-dimensional array and creates a one dimensional
     * array by placing the second dimension arrays one after another.
     * @param array the array to flatten
     * @return the flattened array
     */
    public static int[] flatten(int[][] array) {
        int n = count(array);
        int[] result = new int[n];
        int o = 0;
        for (int[] b : array) {
            System.arraycopy(b, 0, result, o, b.length);
            o += b.length;
        }
        return result;
    }

    /**
     * Takes a two-dimensional array and creates a one dimensional
     * array by placing the second dimension arrays one after another.
     * @param array the array to flatten
     * @return the flattened array
     */
    public static long[] flatten(long[][] array) {
        int n = count(array);
        long[] result = new long[n];
        int o = 0;
        for (long[] b : array) {
            System.arraycopy(b, 0, result, o, b.length);
            o += b.length;
        }
        return result;
    }

    /**
     * Takes a two-dimensional array and creates a one dimensional
     * array by placing the second dimension arrays one after another.
     * @param array the array to flatten
     * @return the flattened array
     */
    public static float[] flatten(float[][] array) {
        int n = count(array);
        float[] result = new float[n];
        int o = 0;
        for (float[] b : array) {
            System.arraycopy(b, 0, result, o, b.length);
            o += b.length;
        }
        return result;
    }

    /**
     * Takes a two-dimensional array and creates a one dimensional
     * array by placing the second dimension arrays one after another.
     * @param array the array to flatten
     * @return the flattened array
     */
    public static double[] flatten(double[][] array) {
        int n = count(array);
        double[] result = new double[n];
        int o = 0;
        for (double[] b : array) {
            System.arraycopy(b, 0, result, o, b.length);
            o += b.length;
        }
        return result;
    }

    /**
     * Takes a two-dimensional array and creates a one dimensional
     * array by placing the second dimension arrays one after another.
     * @param array the array to flatten
     * @return the flattened array
     */
    public static Object[] flatten(Object[][] array) {
        int n = count(array);
        Object[] result = new Object[n];
        int o = 0;
        for (Object[] b : array) {
            System.arraycopy(b, 0, result, o, b.length);
            o += b.length;
        }
        return result;
    }

    /**
     * Counts the total number of elements in the two-dimensional array.
     * The array might be irregular, but not null.
     * @param array the source two dimensional array
     * @return the number of total elements.
     */
    public static int count(boolean[][] array) {
        int c = 0;
        for (boolean[] b : array) {
            c += b.length;
        }
        return c;
    }

    /**
     * Counts the total number of elements in the two-dimensional array.
     * The array might be irregular, but not null.
     * @param array the source two dimensional array
     * @return the number of total elements.
     */
    public static int count(byte[][] array) {
        int c = 0;
        for (byte[] b : array) {
            c += b.length;
        }
        return c;
    }

    /**
     * Counts the total number of elements in the two-dimensional array.
     * The array might be irregular, but not null.
     * @param array the source two dimensional array
     * @return the number of total elements.
     */
    public static int count(short[][] array) {
        int c = 0;
        for (short[] b : array) {
            c += b.length;
        }
        return c;
    }

    /**
     * Counts the total number of elements in the two-dimensional array.
     * The array might be irregular, but not null.
     * @param array the source two dimensional array
     * @return the number of total elements.
     */
    public static int count(char[][] array) {
        int c = 0;
        for (char[] b : array) {
            c += b.length;
        }
        return c;
    }

    /**
     * Counts the total number of elements in the two-dimensional array.
     * The array might be irregular, but not null.
     * @param array the source two dimensional array
     * @return the number of total elements.
     */
    public static int count(int[][] array) {
        int c = 0;
        for (int[] b : array) {
            c += b.length;
        }
        return c;
    }

    /**
     * Counts the total number of elements in the two-dimensional array.
     * The array might be irregular, but not null.
     * @param array the source two dimensional array
     * @return the number of total elements.
     */
    public static int count(long[][] array) {
        int c = 0;
        for (long[] b : array) {
            c += b.length;
        }
        return c;
    }

    /**
     * Counts the total number of elements in the two-dimensional array.
     * The array might be irregular, but not null.
     * @param array the source two dimensional array
     * @return the number of total elements.
     */
    public static int count(float[][] array) {
        int c = 0;
        for (float[] b : array) {
            c += b.length;
        }
        return c;
    }

    /**
     * Counts the total number of elements in the two-dimensional array.
     * The array might be irregular, but not null.
     * @param array the source two dimensional array
     * @return the number of total elements.
     */
    public static int count(double[][] array) {
        int c = 0;
        for (double[] b : array) {
            c += b.length;
        }
        return c;
    }

    /**
     * Counts the total number of elements in the two-dimensional array.
     * The array might be irregular, but not null.
     * @param array the source two dimensional array
     * @return the number of total elements.
     */
    public static int count(Object[][] array) {
        int c = 0;
        for (Object[] b : array) {
            c += b.length;
        }
        return c;
    }
}

Related

  1. flatten(byte[][] matrix)
  2. flatten(double[][] arr)
  3. flatten(double[][] array)
  4. flatten(double[][] array)