[프로그래머스level 0] n의 배수 고르기(120905)-자바(Java)

SolChan Kim·2024년 3월 16일


문제풀이

  • 배열을 IntStream으로 변환하고 배열의 요소를 순회할 준비를 한다.
IntStream.of(numlist)
  • 조건(3의 배수)에 맞는 배열의 요소만 모은다.
IntStream.of(numlist).filter(i-> i % n == 0)
  • filter를 통해 남겨진 배열의 요소들로 새로운 배열을 생성한다.
    • 배열의 타입은 기본형(int)으로 변환됨
IntStream.of(numlist).filter(i-> i % n == 0).toArray();

코드

import java.util.stream.IntStream;

public class Ex_120905 {
  public static int[] solution(int n, int[] numlist) {
    return IntStream.of(numlist).filter(i-> i % n == 0).toArray();
  }
  public static void main(String[] args) {
    int n = 3;
    int[] numlist = {4, 5, 6, 7, 8, 9, 10, 11, 12,};

    System.out.println(solution(n, numlist));
  }
}

0개의 댓글