Study with me/프로그래머스 L0 마스터하기
프로그래머스 - L0 정수찾기
외계나무
2023. 12. 2. 10:10
▼ 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;
}
}