▼ Arrays.asList()를 써보려고 시도한 흔적...
import java.util.*;
class Solution {
public int solution(int[] num_list, int n) {
List<Integer> list = Arrays.asList(Arrays.stream(num_list).boxed().toArray(Integer[]::new));
int answer = list.contains(n) ? 1 : 0;
return answer;
}
}
Arrays.asList()는 기본 데이터 타입의 배열을 지원하지 않기 때문에, int 배열의 경우 Integer 배열로 먼저 변환해 주어야 함.
→ 다음의 작업을 먼저 거쳐야 함.
Arrays.stream( int 배열 ) // 각 원소 분리, int 배열의 스트림화
.boxed() // 래퍼 클래스로 감싸서(boxing) Stream<Integer> 객체로 변환
.toArray(Integer[]::new); // Integer 배열로 변환
▼ 이건 타인의 풀이 (IntStream)
import java.util.stream.IntStream;
class Solution {
public int solution(int[] numList, int n) {
return IntStream.of(numList).anyMatch(num -> num == n) ? 1 : 0;
}
}
'Study with me > 프로그래머스 L0 마스터하기' 카테고리의 다른 글
프로그래머스 - L0 더크게합치기 // String ↔ int (0) | 2023.12.03 |
---|---|
프로그래머스 - L0 수조작하기1 // String → char sequence (0) | 2023.12.02 |
프로그래머스 - L0 조건에맞게수열변환하기3 (0) | 2023.12.01 |
프로그래머스 - L0 마지막두원소 // Array ↔ List (0) | 2023.11.30 |
프로그래머스 - L0 홀짝에따라다른값반환하기 (0) | 2023.11.29 |