나의 풀이
import java.util.*;
class Solution {
public int[] solution(int n, int[] numlist) {
List<Integer> list = new ArrayList<>();
for(int i=0; i<numlist.length; i++) {
if(numlist[i]% n ==0){
list.add(numlist[i]);
}
}
return list.stream().mapToInt(i->i).toArray();
}
}
▶ list.stream().mapToInt(i->i).toArray()
▶ mapToInt( i → i) 리스트의 Integer요소타입을 int타입으로 바꾼다.★★
다른 사람의 풀이
import java.util.Arrays;
class Solution {
public int[] solution(int n, int[] numList) {
return Arrays.stream(numList).filter(value -> value % n == 0).toArray();
}
}
▶ stream의 filter()는 조건에 맞는 값을 추출함★
'프로그래머스(자바) > LV.0(자바)' 카테고리의 다른 글
약수 구하기 - IntStream.rangeClosed(1, n) (0) | 2022.11.24 |
---|---|
가장 큰 수 찾기→max(Integer::compareTo).ortElse(0) ★ (0) | 2022.11.24 |
문자열 정렬하기(2) - String클래스의 toCharArray() (0) | 2022.11.24 |
문자열 정렬하기(1) - replaceAll(), Stream(), mapToInt() (0) | 2022.11.24 |
직각삼각형 출력하기★ - 독특하게 풀 수도 있다. + 이중for문 (0) | 2022.11.24 |