System.arraycopy (src, srcPos, dest, destPos, length)
[dest배열의 destpos번지에 src배열의 소스를 srcpos번지부터 length개 복사]
<삽입의 개념X 덮어씀O>
Object src : 복사하고자 하는 소스입니다. 원본
int srcPos : 위의 원본 소스에서 어느 부분부터 읽어올지 위치를 정해줍니다.
처음부터 데이터를 읽어올거면 0 을 넣어줍니다.
Object dest : 복사할 소스입니다. 복사하려는 대상입니다.
int destPos : 위의 복사본에서 자료를 받을 때, 어느 부분부터 쓸 것인지 시작 위치를 정해줍니다.
처음부터 데이터를 쓸 거면 0 을 넣어줍니다.
int length : 원본에서 복사본으로 데이터를 읽어서 쓸 데이터 길이입니다.
원본에서 복사본까지 얼마큼 읽어올 지 입력하는 것입니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
public static void main(String[] args) {
int[] original = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
int[] arr1 = new int[5];
int[] arr2 = new int[6];
System.arraycopy(original, 0, arr1, 0, arr1.length);
System.arraycopy(original, arr1.length, arr2, 0, arr2.length);
for (int i : arr1) {
System.out.print(i + ", ");
}
System.out.println();
System.out.println("====================");
for (int i : arr2) {
System.out.print(i + ", ");
}
// result
// 1, 2, 3, 4, 5,
// ====================
// 6, 7, 8, 9, 10, 11,
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
public static void main(String[] args) {
char[] abc = { 'A', 'B', 'C', 'D'};
char[] number = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
System.out.println(new String(abc));
System.out.println(new String(number));
// 배열 abc와 number를 붙여서 하나의 배열(result)로 만든다.
char[] result = new char[abc.length+number.length];
System.arraycopy(abc, 0, result, 0, abc.length);
System.arraycopy(number, 0, result, abc.length, number.length);
System.out.println(new String(result));
// 배열 abc을 배열 number의 첫 번째 위치부터 배열 abc의 크기만큼 복사
System.arraycopy(abc, 0, number, 0, abc.length);
System.out.println(new String(number));
// number의 인덱스6 위치에 3개를 복사
System.arraycopy(abc, 0, number, 6, 3);
System.out.println(new String(number));
// 출력
/*
ABCD
0123456789
ABCD0123456789
ABCD456789
ABCD45ABC9
*/
}
|
cs |
'JAVA > F' 카테고리의 다른 글
[자바] 정확한 실수 표현 BigInteger / BigDecimal (0) | 2020.10.22 |
---|---|
[자바] wrapper 클래스 (0) | 2020.10.22 |
[자바] instanceof (0) | 2020.10.20 |
[자바] String 관련 함수/메소드 (0) | 2020.10.13 |
[자바] 비어있는지 확인하는 함수 / isEmpty() (0) | 2020.10.12 |
[자바] 앞 뒤 스페이스 / 공백 제거 함수 : trim() (0) | 2020.10.12 |
[자바] Math.abs() / 절대값 함수 (0) | 2020.10.07 |