Skip to content

Instantly share code, notes, and snippets.

@Yur-ok
Created January 12, 2016 10:29
Show Gist options
  • Select an option

  • Save Yur-ok/6826c85692f80d5fc31e to your computer and use it in GitHub Desktop.

Select an option

Save Yur-ok/6826c85692f80d5fc31e to your computer and use it in GitHub Desktop.
package Lesson3.KeyPoint5;
import java.util.Arrays;
/**
* Created by Юрий on 08.01.2016.
*/
public class CopyArray {
public static void main(String[] args) {
int[] one = {1, 2, 9, 33, 44};
int[] two = {4, 5, 6};
copyArray(one, 0, two, 0, 2);
}
static void copyArray(int[] src, int srcPos, int[] dest, int destPos, int length) {
int restDest = dest.length - (destPos + length);
if (restDest < 0) {
System.out.println(-1);
} else {
for (int i = srcPos, k = destPos; length > 0; i++, k++) {
dest[k] = src[i];
length--;
}
System.out.println(Arrays.toString(dest));
}
}
}
@liuiv15
Copy link
Copy Markdown

liuiv15 commented Jan 21, 2016

  1. Если один из массивов null, будет ошибка.
  2. если параметры srcPos, dest, length будет отрицательным, будет ошибка.
  3. если сумма (srcPos+length) выйдет за границы массива, будет ошибка

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment