728x90
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
3. int 와 Integer의 차이점
int는 자료형
- 산술 연산 가능
- null로 초기화 불가능
- 저장공간이 4Byte라고 작음
Integer
- Unboxing하지 않을 시 산술 연산이 불가능
- null 값으로 처리 가능
- 저장공간이 큼
- null값으로 처리가 가능해 SQL에 용이하게 쓰인다.
https://smin1620.tistory.com/287
[Java] int와 Integer는 뭐가 다를까?
최근에 자바를 공부하고 있는데, 공부하면서 이상한 점을 발견했다. 파이썬에서는 고민조차 안했던 건데;;; int와 Integer는 대체 무슨 차이일까? 자바 코드 아키텍처도 살짝 다르게 작성하던데 왜
smin1620.tistory.com
import java.util.Arrays;
import java.util.Scanner;
class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int tc = 1; tc <= t; tc++) {
int sum = 0;
int n = sc.nextInt();
int k = sc.nextInt();
Integer arr[] = new Integer[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
Arrays.sort(arr, (o1, o2) -> Integer.compare(o2, o1));
// Arrays.sort(arr, Collections.reverseOrder());
for (int i = 0; i < k; i++) {
sum += arr[i];
}
System.out.println("#" + tc + " " + sum);
}
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int tc = 1; tc <= t; tc++) {
int sum = 0;
int n = sc.nextInt();
int k = sc.nextInt();
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
list.add(sc.nextInt());
}
Collections.sort(list, Collections.reverseOrder());
// Collections.sort(list, (o1, o2) -> Integer.compare(o2, o1));
for (int i = 0; i < k; i++) {
sum += list.get(i);
}
System.out.println("#" + tc + " " + sum);
}
}
}
o2 o1
10 20 배열에 있다고 가정하면
20 - 10 양수니까 바꿈
728x90
'SW Expert Academy > SWEA D3' 카테고리의 다른 글
| [SW Expert Academy] 두가지 빵의 딜레마 (D3) (0) | 2024.09.03 |
|---|---|
| [SW Expert Academy] 적고 지우기 (D3) (0) | 2024.09.03 |
| [SW Expert Academy] 통나무 자르기 (D3) (0) | 2024.09.03 |
| [SW Expert Academy] 농작물 수확하기 (D3) (0) | 2024.09.03 |
| [SW Expert Academy] [S/W 문제해결 기본] 10일차 - 비밀번호 (D3) (0) | 2024.09.02 |