Java Array Copy arraycopy(char[] A1, int offset1, char[] A2, int offset2, int length)

Here you can find the source of arraycopy(char[] A1, int offset1, char[] A2, int offset2, int length)

Description

Copies the contents of A1 starting at offset offset1 into A2 starting at offset offset2 for length elements.

License

Open Source License

Parameter

Parameter Description
A1 the array to copy out of
offset1 the starting index in array1
A2 the array to copy into
offset2 the starting index in array2
length the number of elements in the array to copy

Declaration

static void arraycopy(char[] A1, int offset1, char[] A2, int offset2,
        int length) 

Method Source Code

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

public class Main {
    /**/*  w  w w.j  a va2s  . c om*/
     * Copies the contents of <code>A1</code> starting at offset
     * <code>offset1</code> into <code>A2</code> starting at offset
     * <code>offset2</code> for <code>length</code> elements.
     * 
     * @param A1 the array to copy out of
     * @param offset1 the starting index in array1
     * @param A2 the array to copy into
     * @param offset2 the starting index in array2
     * @param length the number of elements in the array to copy
     */
    static void arraycopy(char[] A1, int offset1, char[] A2, int offset2,
            int length) {
        if (offset1 >= 0 && offset2 >= 0 && length >= 0
                && length <= A1.length - offset1
                && length <= A2.length - offset2) {
            // Check if this is a forward or backwards arraycopy
            if (A1 != A2 || offset1 > offset2
                    || offset1 + length <= offset2) {
                for (int i = 0; i < length; ++i) {
                    A2[offset2 + i] = A1[offset1 + i];
                }
            } else {
                for (int i = length - 1; i >= 0; --i) {
                    A2[offset2 + i] = A1[offset1 + i];
                }
            }
        } else {
            throw new ArrayIndexOutOfBoundsException();
        }
    }

    /**
     * Copies the contents of <code>A1</code> into <code>A2</code> for <code>length</code> elements.
     * 
     * @param A1 the array to copy out of
     * @param A2 the array to copy into
     * @param length the number of elements in the array to copy
     */
    static void arraycopy(char[] A1, char[] A2, int length) {
        if (length >= 0 && length <= A1.length && length <= A2.length) {
            for (int i = 0; i < length; ++i) {
                A2[i] = A1[i];
            }
        } else {
            throw new ArrayIndexOutOfBoundsException();
        }
    }
}

Related

  1. arrayCopy(byte[] source, byte[] dest)
  2. arraycopy(byte[] src, int destLen, int len)
  3. arraycopy(byte[] src, int src_position, byte[] dst, int dst_position, int length)
  4. arrayCopy(byte[] src, int srcOffset, byte[] target, int targetOffset, int length)
  5. arrayCopy(byte[] src, int srcStart, byte[] dest, int destStart, int destBitOffset, int lengthInBits)
  6. arrayCopy(char[] chars)
  7. arrayCopy(double[][] src)
  8. arrayCopy(final byte[] src, final byte[] dest)
  9. arrayCopy(final float[] arrayToCopy)