Copies the given source array into the required column number of the destination - Java java.lang

Java examples for java.lang:Math Array Function

Description

Copies the given source array into the required column number of the destination

Demo Code

/*//from w w  w .  j a v a  2 s. c om
 *  Java Information Dynamics Toolkit (JIDT)
 *  Copyright (C) 2012, Joseph T. Lizier
 *  
 *  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/>.
 */
//package com.java2s;

public class Main {
    /**
     * Copies the given source array into the required column number of the destination
     * @param destination
     * @param column
     * @param source
     */
    public static void copyIntoColumn(int[][] destination, int column,
            int[] source) throws Exception {
        if (source.length != destination.length) {
            throw new Exception(
                    "Destination column is not of the same length as the source ("
                            + destination.length + " vs " + source.length
                            + ")");
        }
        for (int r = 0; r < destination.length; r++) {
            destination[r][column] = source[r];
        }
    }

    /**
     * Copies the given source array into the required column number of the destination
     * @param destination
     * @param column
     * @param source
     */
    public static void copyIntoColumn(double[][] destination, int column,
            int destFromRowNumber, double[] source,
            int sourceFromRowNumber, int rows) throws Exception {
        if (sourceFromRowNumber + rows > source.length) {
            throw new Exception("Attempting to copy too many rows " + rows
                    + " after the start row " + sourceFromRowNumber
                    + " from the source of length " + source.length);
        }
        if (destFromRowNumber + rows > destination.length) {
            throw new Exception("Attempting to copy too many rows " + rows
                    + " after the start row " + destFromRowNumber
                    + " from the destination of length "
                    + destination.length);
        }
        for (int r = 0; r < rows; r++) {
            destination[r + destFromRowNumber][column] = source[r
                    + sourceFromRowNumber];
        }
    }

    /**
     * Copies the given source array into the required column number of the destination
     * @param destination
     * @param column
     * @param source
     */
    public static void copyIntoColumn(int[][] destination, int column,
            int destFromRowNumber, int[] source, int sourceFromRowNumber,
            int rows) throws Exception {
        if (sourceFromRowNumber + rows > source.length) {
            throw new Exception("Attempting to copy too many rows " + rows
                    + " after the start row " + sourceFromRowNumber
                    + " from the source of length " + source.length);
        }
        if (destFromRowNumber + rows > destination.length) {
            throw new Exception("Attempting to copy too many rows " + rows
                    + " after the start row " + destFromRowNumber
                    + " from the destination of length "
                    + destination.length);
        }
        for (int r = 0; r < rows; r++) {
            destination[r + destFromRowNumber][column] = source[r
                    + sourceFromRowNumber];
        }
    }

    /**
     * Copies the given source array into the required column number of the destination
     * @param destination
     * @param column
     * @param source
     */
    public static void copyIntoColumn(double[][] destination, int column,
            double[] source) throws Exception {
        if (source.length != destination.length) {
            throw new Exception(
                    "Destination column is not of the same length as the source ("
                            + destination.length + " vs " + source.length
                            + ")");
        }
        for (int r = 0; r < destination.length; r++) {
            destination[r][column] = source[r];
        }
    }
}

Related Tutorials