「Java入門」System.arraycopy()で配列をコピーするサンプル

2018年2月10日

Javaコード
package com.arkgame.study;
public class SystemArrCopyDemo {
public static void main(String[] args) {
int[] cft1 = { 22, 33, 44, 55, 66};
int[] cft2 = new int[5];
System.arraycopy(cft1, 0, cft2, 0, 5);

System.out.println(“配列 cft1″);
for (int acft1 : cft1) {
System.out.print(acft1 + " , “);
}

cft2[2] = 111;
System.out.println(“\n配列 cft2″);
for (int acft2 : cft2) {
System.out.print(acft2 + " , “);
}
System.out.println(“\n配列 cft1″);
for (int acft1 : cft1) {
System.out.print(acft1 + " , “);
}

System.out.println(“\n———————–“);
int[][] cft3 = { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 } };
int[][] cft4 = new int[cft3.length][cft3[0].length];
System.out.println(“配列 cft3");
System.arraycopy(cft3, 0, cft4, 0, cft3.length);
for (int[] acft3 : cft3) {
for (int j = 0; j < cft4[0].length; j++) {
System.out.print(acft3[j] + “,");
}
}

cft4[1][3] = 111;
System.out.println(“\n配列 cft4");
for (int[] acft4 : cft4) {
for (int j = 0; j < cft4[0].length; j++) {
System.out.print(acft4[j] + “,");
}
}
System.out.println(“\n配列 cft3");
for (int[] acft3 : cft3) {
for (int j = 0; j < cft4[0].length; j++) {
System.out.print(acft3[j] + “,");
}
}

}

}

実行結果
配列 cft1
22 , 33 , 44 , 55 , 66 ,
配列 cft2
22 , 33 , 111 , 55 , 66 ,
配列 cft1
22 , 33 , 44 , 55 , 66 ,
———————–
配列 cft3
1,2,3,4,5,6,7,8,9,10,
配列 cft4
1,2,3,4,5,6,7,8,111,10,
配列 cft3
1,2,3,4,5,6,7,8,111,10,

Java

Posted by arkgame