Description
Copies elements from the
src
array to the
dest
array.
License
Apache License
Parameter
Parameter | Description |
---|
src | the source array |
srcPos | the starting position in the source array |
dest | the destination array |
destPos | the starting position in the destination array |
length | the number of array elements to copy |
Exception
Parameter | Description |
---|
NullPointerException | if <code>src</code> or <code>dest</code> is <code>null</code> |
IndexOutOfBoundsException | if <code>srcPos < 0 || destPos < 0 || length < 0 || (srcPos + length > src.length) || (destPos + length > dest.length)</code> |
ArrayStoreException | if an element in <code>src</code> cannot not be copied to <code>dest</code> due to a type mismatch |
Declaration
public static void arrayCopy(Object src, int srcPos, Object dest, int destPos, int length)
Method Source Code
//package com.java2s;
/*//from w ww . java 2 s .com
* Copyright 2006 Mat Gessel <mat.gessel@gmail.com>
*
* 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 {
/**
* Copies elements from the <code>src</code> array to the <code>dest</code> array.
*
* @param src the source array
* @param srcPos the starting position in the source array
* @param dest the destination array
* @param destPos the starting position in the destination array
* @param length the number of array elements to copy
* @throws NullPointerException if <code>src</code> or <code>dest</code> is <code>null</code>
* @throws IndexOutOfBoundsException if <code>srcPos < 0 || destPos < 0 || length < 0 || (srcPos + length > src.length) || (destPos + length > dest.length)</code>
* @throws ArrayStoreException if an element in <code>src</code> cannot not be copied to <code>dest</code> due to a type mismatch
* @see System#arraycopy(Object, int, Object, int, int)
*/
public static void arrayCopy(Object src, int srcPos, Object dest, int destPos, int length) {
// need to check srcPos and destPos for script mode
if (length < 0 || srcPos < 0 || destPos < 0)
throw new IndexOutOfBoundsException();
if (src instanceof Object[] && dest instanceof Object[]) {
arrayCopy((Object[]) src, srcPos, (Object[]) dest, destPos, length);
} else if (src instanceof int[] && dest instanceof int[]) {
arrayCopy((int[]) src, srcPos, (int[]) dest, destPos, length);
} else if (src instanceof boolean[] && dest instanceof boolean[]) {
arrayCopy((boolean[]) src, srcPos, (boolean[]) dest, destPos, length);
} else if (src instanceof float[] && dest instanceof float[]) {
arrayCopy((float[]) src, srcPos, (float[]) dest, destPos, length);
} else if (src instanceof char[] && dest instanceof char[]) {
arrayCopy((char[]) src, srcPos, (char[]) dest, destPos, length);
} else if (src instanceof byte[] && dest instanceof byte[]) {
arrayCopy((byte[]) src, srcPos, (byte[]) dest, destPos, length);
} else if (src instanceof short[] && dest instanceof short[]) {
arrayCopy((short[]) src, srcPos, (short[]) dest, destPos, length);
} else if (src instanceof long[] && dest instanceof long[]) {
arrayCopy((long[]) src, srcPos, (long[]) dest, destPos, length);
} else if (src instanceof double[] && dest instanceof double[]) {
arrayCopy((double[]) src, srcPos, (double[]) dest, destPos, length);
} else if (src == null || dest == null) {
throw new NullPointerException();
} else {
throw new ArrayStoreException();
}
}
private static void arrayCopy(Object[] src, int srcPos, Object[] dest, int destPos, int length) {
if (srcPos + length > src.length || destPos + length > dest.length)
throw new IndexOutOfBoundsException();
for (int i = 0; i < length; i++) {
dest[destPos + i] = src[srcPos + i];
}
}
private static void arrayCopy(int[] src, int srcPos, int[] dest, int destPos, int length) {
if (srcPos + length > src.length || destPos + length > dest.length)
throw new IndexOutOfBoundsException();
for (int i = 0; i < length; i++) {
dest[destPos + i] = src[srcPos + i];
}
}
private static void arrayCopy(boolean[] src, int srcPos, boolean[] dest, int destPos, int length) {
if (srcPos + length > src.length || destPos + length > dest.length)
throw new IndexOutOfBoundsException();
for (int i = 0; i < length; i++) {
dest[destPos + i] = src[srcPos + i];
}
}
private static void arrayCopy(float[] src, int srcPos, float[] dest, int destPos, int length) {
if (srcPos + length > src.length || destPos + length > dest.length)
throw new IndexOutOfBoundsException();
for (int i = 0; i < length; i++) {
dest[destPos + i] = src[srcPos + i];
}
}
private static void arrayCopy(char[] src, int srcPos, char[] dest, int destPos, int length) {
if (srcPos + length > src.length || destPos + length > dest.length)
throw new IndexOutOfBoundsException();
for (int i = 0; i < length; i++) {
dest[destPos + i] = src[srcPos + i];
}
}
private static void arrayCopy(byte[] src, int srcPos, byte[] dest, int destPos, int length) {
if (srcPos + length > src.length || destPos + length > dest.length)
throw new IndexOutOfBoundsException();
for (int i = 0; i < length; i++) {
dest[destPos + i] = src[srcPos + i];
}
}
private static void arrayCopy(short[] src, int srcPos, short[] dest, int destPos, int length) {
if (srcPos + length > src.length || destPos + length > dest.length)
throw new IndexOutOfBoundsException();
for (int i = 0; i < length; i++) {
dest[destPos + i] = src[srcPos + i];
}
}
private static void arrayCopy(long[] src, int srcPos, long[] dest, int destPos, int length) {
if (srcPos + length > src.length || destPos + length > dest.length)
throw new IndexOutOfBoundsException();
for (int i = 0; i < length; i++) {
dest[destPos + i] = src[srcPos + i];
}
}
private static void arrayCopy(double[] src, int srcPos, double[] dest, int destPos, int length) {
if (srcPos + length > src.length || destPos + length > dest.length)
throw new IndexOutOfBoundsException();
for (int i = 0; i < length; i++) {
dest[destPos + i] = src[srcPos + i];
}
}
}
Related
- arrayCopy(int[] x)
- arrayCopy(int[][] source, int[][] destination)
- arraycopy(long src[], int srcOffset, long dest[], int destOffset, int limit)
- arraycopy(long[] src, int srcPos, long[] dest, int destPos, int length)
- arraycopy(Object dest, Object src1, int length1, Object src2, int length2)
- arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
- arrayCopy(Object[] source, Object[] target, int size)
- arraycopy(Object[] src, int srcPos, Object[] dest, int destPos, int length)
- arraycopy(String[] src)