1. 고전적인 System.arraycopy
Java에서 각종 array를 복사하는 경우에 사용되었던 방식이다.
입력받을 값에 미리 초기화해 놓아야 한다.
int[] array = new int[] { 1, 2, 3, 4 };
int[] copy = new int[4];
System.arraycopy(array, 0, copy, 0, 4);
그리고 source와 destination의 시작위치를 지정할 수 있지만,
경험상 잘 사용할 필요가 거의 없었다
2. 새로운 java.util.Arrays.copyOf (JDK6)
int[] array = new int[] { 1, 2, 3, 4 };
int[] copy = Arrays.copyOf(array, array.length);
int[] other = Arrays.copyOf(array, 10);
입력 받을 값을 미리 세팅할 필요가 없다. 파라미터도 명시적이고...
other의 경우는 other[4] ~ other[9]가 0으로 초기화된 길이가 10인 int[]로 생성된다 (Object라면 null이 들어간 array로)
(System.arraycopy와 동일한 Arrays.copyOfRange도 있다)
3. 성능 비교 ( clone vs. Arrays.copyOf )
df
Clone : 3156
arraycopy: 3187
copyOf : 3157
셋 다 거의 비슷한 성능을 보여주었다.
결국 사용성이 편한 Arrays.copyOf의 승리
출처 : http://vczx.tistory.com/
[본문링크] Arrays.copyOf vs. System.arraycopy
'Programming Language > JAVA' 카테고리의 다른 글
Java에서 system.out / data type / method / scope rule 특징 (0) | 2013.03.12 |
---|