Skip to content

Commit 95b7b5a

Browse files
committed
🚀[feat] 자바 - 배열복사
1 parent e4fc404 commit 95b7b5a

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package practiceJAVA;
2+
3+
public class CopyOfArray {
4+
public static void main(String[] args) {
5+
6+
int [] arrInt = {1,2,3,4,5};
7+
String [] arrStr = {"최수정","차수림","윤원빈","허진주"};
8+
9+
int [] arrIntCopired = new int[5]; // 복사 대상보다 같거나 크게 공간 설정
10+
String [] arrStrCopierd = new String[5];
11+
12+
/**
13+
* basic : for문
14+
*/
15+
for(int i=0; i<arrInt.length; i++){
16+
arrIntCopired[i] = arrInt[i];
17+
}
18+
19+
System.out.print("basic copy result: ");
20+
printInt(arrIntCopired);
21+
22+
/**
23+
* arraycopy 메서드 활용
24+
*/
25+
System.arraycopy(arrStr, 0, arrStrCopierd, 0, arrStr.length);
26+
27+
System.out.print("arraycopy method result: ");
28+
printStr(arrStrCopierd);
29+
30+
/**
31+
* for each문 활용
32+
*/
33+
int sum = 0;
34+
for (int a : arrInt) {
35+
sum += a;
36+
}
37+
System.out.printf("for-each문 sum 구하기: %d \n", sum);
38+
39+
}
40+
41+
public static void printInt(int[] arr) {
42+
for(int i=0; i<arr.length; i++) {
43+
System.out.print(arr[i] + ", ");
44+
}
45+
System.out.println();
46+
}
47+
48+
public static void printStr(String [] arr) {
49+
for(int i=0; i<arr.length; i++) {
50+
System.out.print(arr[i] + ", ");
51+
}
52+
System.out.println();
53+
}
54+
55+
}

0 commit comments

Comments
 (0)