Java Matrix Flatten flatten(byte[][] matrix)

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

Description

Converts the matrix into a flat array, row after row.

License

Open Source License

Parameter

Parameter Description
matrix the matrix to convert

Return

the matrix as arrawy (row after row)

Declaration

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

Method Source Code

//package com.java2s;
/*//from   www. j a  v  a  2  s  .  c o m
 *   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/>.
 */

public class Main {
    /**
     * Converts the matrix into a flat array, row after row.
     *
     * @param matrix   the matrix to convert
     * @return      the matrix as arrawy (row after row)
     */
    public static byte[] flatten(byte[][] matrix) {
        byte[] result;
        int i;
        int n;
        int len;

        len = 0;
        for (i = 0; i < matrix.length; i++)
            len += matrix[i].length;

        result = new byte[len];
        len = 0;
        for (i = 0; i < matrix.length; i++) {
            for (n = 0; n < matrix[i].length; n++)
                result[len + n] = matrix[i][n];
            len += matrix[i].length;
        }

        return result;
    }

    /**
     * Converts the matrix into a flat array, row after row.
     *
     * @param matrix   the matrix to convert
     * @return      the matrix as arrawy (row after row)
     */
    public static short[] flatten(short[][] matrix) {
        short[] result;
        int i;
        int n;
        int len;

        len = 0;
        for (i = 0; i < matrix.length; i++)
            len += matrix[i].length;

        result = new short[len];
        len = 0;
        for (i = 0; i < matrix.length; i++) {
            for (n = 0; n < matrix[i].length; n++)
                result[len + n] = matrix[i][n];
            len += matrix[i].length;
        }

        return result;
    }

    /**
     * Converts the matrix into a flat array, row after row.
     *
     * @param matrix   the matrix to convert
     * @return      the matrix as arrawy (row after row)
     */
    public static int[] flatten(int[][] matrix) {
        int[] result;
        int i;
        int n;
        int len;

        len = 0;
        for (i = 0; i < matrix.length; i++)
            len += matrix[i].length;

        result = new int[len];
        len = 0;
        for (i = 0; i < matrix.length; i++) {
            for (n = 0; n < matrix[i].length; n++)
                result[len + n] = matrix[i][n];
            len += matrix[i].length;
        }

        return result;
    }

    /**
     * Converts the matrix into a flat array, row after row.
     *
     * @param matrix   the matrix to convert
     * @return      the matrix as arrawy (row after row)
     */
    public static long[] flatten(long[][] matrix) {
        long[] result;
        int i;
        int n;
        int len;

        len = 0;
        for (i = 0; i < matrix.length; i++)
            len += matrix[i].length;

        result = new long[len];
        len = 0;
        for (i = 0; i < matrix.length; i++) {
            for (n = 0; n < matrix[i].length; n++)
                result[len + n] = matrix[i][n];
            len += matrix[i].length;
        }

        return result;
    }

    /**
     * Converts the matrix into a flat array, row after row.
     *
     * @param matrix   the matrix to convert
     * @return      the matrix as arrawy (row after row)
     */
    public static float[] flatten(float[][] matrix) {
        float[] result;
        int i;
        int n;
        int len;

        len = 0;
        for (i = 0; i < matrix.length; i++)
            len += matrix[i].length;

        result = new float[len];
        len = 0;
        for (i = 0; i < matrix.length; i++) {
            for (n = 0; n < matrix[i].length; n++)
                result[len + n] = matrix[i][n];
            len += matrix[i].length;
        }

        return result;
    }

    /**
     * Converts the matrix into a flat array, row after row.
     *
     * @param matrix   the matrix to convert
     * @return      the matrix as arrawy (row after row)
     */
    public static double[] flatten(double[][] matrix) {
        double[] result;
        int i;
        int n;
        int len;

        len = 0;
        for (i = 0; i < matrix.length; i++)
            len += matrix[i].length;

        result = new double[len];
        len = 0;
        for (i = 0; i < matrix.length; i++) {
            for (n = 0; n < matrix[i].length; n++)
                result[len + n] = matrix[i][n];
            len += matrix[i].length;
        }

        return result;
    }

    /**
     * Converts the matrix into a flat array, row after row.
     *
     * @param matrix   the matrix to convert
     * @return      the matrix as arrawy (row after row)
     */
    public static Number[] flatten(Number[][] matrix) {
        Number[] result;
        int i;
        int n;
        int len;

        len = 0;
        for (i = 0; i < matrix.length; i++)
            len += matrix[i].length;

        result = new Number[len];
        len = 0;
        for (i = 0; i < matrix.length; i++) {
            for (n = 0; n < matrix[i].length; n++)
                result[len + n] = matrix[i][n];
            len += matrix[i].length;
        }

        return result;
    }
}

Related

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