본문 바로가기
Study with me/프로그래머스 L0 마스터하기

프로그래머스 - L0 n의배수고르기

by 외계나무 2023. 11. 21.

프로그래머스 - level 0 n의 배수 고르기

import java.util.*;

class Solution {
    public Integer[] solution(int n, int[] numlist) {
        List<Integer> list = new ArrayList<>();
        for(int num : numlist) {
            if(num%n==0) {
                list.add(num);
            }
        }
        Integer[] answer = new Integer[list.size()];
        list.toArray(answer);
        return answer;
    }
}

Stream을 쓰고 싶어서 시도했으나 잘 안되어... 그냥 ArrayList 사용.

그래서 stream을 쓴 코드를 가져와 봤다.

import java.util.Arrays;

class Solution {
    public int[] solution(int n, int[] numList) {
        return Arrays.stream(numList).filter(value -> value % n == 0).toArray();
    }
}