Java Array Clone clone(int[] src, int[] dest)

Here you can find the source of clone(int[] src, int[] dest)

Description

Copies source array content to destination array.

License

Open Source License

Parameter

Parameter Description
src Source array
dest Destination array <p>

Return

Destinarion array

Declaration

public static int[] clone(int[] src, int[] dest) 

Method Source Code

//package com.java2s;
/* /*  w  ww.ja va  2  s .  c o  m*/
 * The MIT License
 *
 * Copyright (C) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

public class Main {
    /**
     * Copies source array content to destination array.
     * <p>
     * @param src  Source array
     * @param dest Destination array
     * <p>
     * @return Destinarion array
     */
    public static int[] clone(int[] src, int[] dest) {
        if (src.length != dest.length) {
            throw new IllegalArgumentException("Arrays differ in sizes!");
        }
        System.arraycopy(src, 0, dest, 0, src.length);

        return dest;
    }

    /**
     * Copies source array content to destination array.
     * <p>
     * @param <T>  Array type
     * @param src  Source array
     * @param dest Destination array
     * <p>
     * @return Destinarion array
     */
    public static <T> T[] clone(T[] src, T[] dest) {
        if (src.length != dest.length) {
            throw new IllegalArgumentException("Arrays differ in sizes!");
        }
        System.arraycopy(src, 0, dest, 0, src.length);

        return dest;
    }

    /**
     * Copies source array content to destination array.
     * <p>
     * @param src  Source array
     * @param dest Destination array
     * <p>
     * @return Destinarion array
     */
    public static int[][] clone(int[][] src, int[][] dest) {
        if (!checkEqualLength(src, dest)) {
            throw new IllegalArgumentException("Arrays differ in sizes!");
        }
        for (int i = 0; i < src.length; i++) {
            System.arraycopy(src[i], 0, dest[i], 0, src[i].length);
        }
        return dest;
    }

    /**
     * Copies source array content to destination array.
     * <p>
     * @param <T>  Array type
     * @param src  Source array
     * @param dest Destination array
     * <p>
     * @return Destinarion array
     */
    public static <T> T[][] clone(T[][] src, T[][] dest) {
        if (!checkEqualLength(src, dest)) {
            throw new IllegalArgumentException("Arrays differ in sizes!");
        }
        for (int i = 0; i < src.length; i++) {
            System.arraycopy(src[i], 0, dest[i], 0, src[i].length);
        }
        return dest;
    }

    /**
     * Copies source array content to destination array.
     * <p>
     * @param src  Source array
     * @param dest Destination array
     * <p>
     * @return Destinarion array
     */
    public static int[][][] clone(int[][][] src, int[][][] dest) {
        if (!checkEqualLength(src, dest)) {
            throw new IllegalArgumentException("Arrays differ in sizes!");
        }
        for (int i = 0; i < src.length; i++) {
            for (int j = 0; j < src[i].length; j++) {
                System.arraycopy(src[i][j], 0, dest[i][j], 0, src[i][j].length);
            }
        }
        return dest;
    }

    /**
     * Copies source array content to destination array.
     * <p>
     * @param <T>  Array type
     * @param src  Source array
     * @param dest Destination array
     * <p>
     * @return Destinarion array
     */
    public static <T> T[][][] clone(T[][][] src, T[][][] dest) {
        if (!checkEqualLength(src, dest)) {
            throw new IllegalArgumentException("Arrays differ in sizes!");
        }
        for (int i = 0; i < src.length; i++) {
            for (int j = 0; j < src[i].length; j++) {
                System.arraycopy(src[i][j], 0, dest[i][j], 0, src[i][j].length);
            }
        }
        return dest;
    }

    /**
     * Checks if 2 arrays have equal length. Works with non-rectangular arrays.
     * <p>
     * @param <T>  Array type
     * @param arr1 Array 1
     * @param arr2 Array 2
     * <p>
     * @return true if length is equal, false otherwise.
     */
    public static <T> boolean checkEqualLength(T[][] arr1, T[][] arr2) {
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i].length != arr2[i].length) {
                return false;
            }
        }
        return true;
    }

    /**
     * Checks if 2 arrays have equal length. Works with non-rectangular arrays.
     * <p>
     * @param arr1 Array 1
     * @param arr2 Array 2
     * <p>
     * @return true if length is equal, false otherwise.
     */
    public static boolean checkEqualLength(int[][] arr1, int[][] arr2) {
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i].length != arr2[i].length) {
                return false;
            }
        }
        return true;
    }

    /**
     * Checks if 2 arrays have equal length. Works with non-rectangular arrays.
     * <p>
     * @param <T>  Array type
     * @param arr1 Array 1
     * @param arr2 Array 2
     * <p>
     * @return true if length is equal, false otherwise.
     */
    public static <T> boolean checkEqualLength(T[][][] arr1, T[][][] arr2) {
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i].length != arr2[i].length) {
                return false;
            }
        }
        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr1[i].length; j++) {
                if (arr1[i][j].length != arr2[i][j].length) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * Checks if 2 arrays have equal length. Works with non-rectangular arrays.
     * <p>
     * @param arr1 Array 1
     * @param arr2 Array 2
     * <p>
     * @return true if length is equal, false otherwise.
     */
    public static boolean checkEqualLength(int[][][] arr1, int[][][] arr2) {
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i].length != arr2[i].length) {
                return false;
            }
        }
        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr1[i].length; j++) {
                if (arr1[i][j].length != arr2[i][j].length) {
                    return false;
                }
            }
        }
        return true;
    }
}

Related

  1. clone(int[] array)
  2. clone(int[] array)
  3. clone(int[] array)
  4. clone(int[] in)
  5. clone(int[] src)
  6. clone(Object[] array)
  7. clone(Object[] array)
  8. clone(Object[] array)
  9. clone(Object[] source)