Return to Snippet

Revision: 37308
at December 8, 2010 07:29 by Voil


Initial Code
public void arraycopy(char[] source, int copyFrom, char[] target, int copyTo, int length) {
	// Input validation
	if(source == null || target == null)
		return;
	if(copyFrom+length >= source.length || copyTo+length >	source.length)
		return;
	// Same as: System.arraycopy(source, copyFrom, target, copyTo, length);
	for(int i=0;i<length;i++) {
		target[copyTo+i] = source[copyFrom+i];
	}
}

Initial URL

                                

Initial Description
Does the same as `System.arraycopy(source, copyFrom ,target, copyTo, length)` but without worrying about invalid input. Example for an array of characters.

Initial Title
Safe `arraycopy` Method

Initial Tags
array

Initial Language
Java