Notice
Recent Posts
Recent Comments
Link
개발 공부~
[자바] Array.sort()와 clone() 본문
Array.sort()
- 원본 배열 자체를 정렬시킨다
public static void main(String[] args) {
int[] tmp = {4, 2, 3, 1, 5};
int[] sorted = solution(tmp);
System.out.println(Arrays.toString(tmp)); //[1,2,3,4,5]
System.out.println(Arrays.toString(sorted)); //[1,2,3,4,5]
}
private static int[] solution(int[] arr){
Arrays.sort(arr);
return arr;
}
clone()을 이용한 정렬
- 원본 배열의 상태를 유지하면서 원본 배열로부터 새로운 배열을 복사해서 사용해야 되는 상황에서 사
public static void main(String[] args) {
int[] tmp = {4, 2, 3, 1, 5};
int[] sorted = solution(tmp);
System.out.println(Arrays.toString(tmp)); //[4,2,3,1,5]
System.out.println(Arrays.toString(sorted)); //[1,2,3,4,5]
}
private static int[] solution(int[] arr){
int[] clone = arr.clone();
Arrays.sort(clone);
return clone;
}
'IT > JAVA' 카테고리의 다른 글
그래프와 인접행렬 (0) | 2024.11.14 |
---|---|
[자바] replaceAll("정규식","대체문자") (0) | 2024.09.23 |
[자바] StringBuffer, StringBuilder (0) | 2024.08.07 |
[자바] 해시맵 (0) | 2024.08.07 |
[알고리즘] 그래프 (0) | 2024.08.05 |