「Java入門」System.arraycopy()のサンプル

Javaコード
package com.arkgame.study;
import java.util.Arrays;

public class ArraycopyDemo {

public static void main(String[] args) {
int[] ctn = { 12, 24, 56, 78, 51 };
System.out.println(Arrays.toString(ctn));
System.arraycopy(ctn, 0, ctn, 3, 2);
System.out.println(Arrays.toString(ctn));

int[] ctn2 = new int[6];
System.arraycopy(ctn, 1, ctn2, 0, 3);
System.out.println(Arrays.toString(ctn2));

try {
System.arraycopy(ctn, 0, ctn2, 0, ctn.length + 1);
} catch (IndexOutOfBoundsException ex) {

System.out.println(“occur IndexOutOfBoundsException “);
}
System.out.println(Arrays.toString(ctn2));
}

}
実行結果
[12, 24, 56, 78, 51]
[12, 24, 56, 12, 24]
[24, 56, 12, 0, 0, 0]
occur IndexOutOfBoundsException
[24, 56, 12, 0, 0, 0]

Comments are closed.