본문 바로가기
JAVA/Coding Test Study

[Lv.0] 프로그래머스 - ad 제거하기 : Java

by ♡˖GYURI˖♡ 2024. 4. 16.
728x90
 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

 

이해하기

다 풀어놓고 list를 String 배열로 바꾸는 stream식을 몰라서 틀렸다 ㅋㅋ...

이번 기회에 정리해둬야지 ㅜㅅㅜ

 

참고한 블로그⬇️

 

[Java/자바] List와 String(문자열, 배열)을 서로 변환하는 법

List ↔ String List를 String으로 간단히 변환 join() 메서드 활용 List list = new ArrayList(); String answer = String.join(",",list); String을 List로 변환 split() 메서드 활용 String s = "sample"; String[] strArr = s.split(""); // [s, a

hstory0208.tistory.com

 

list.stream().toArray(String[]::new)

 

 

문제풀이

import java.util.*;

class Solution {
    public String[] solution(String[] strArr) {
        List<String> list = new ArrayList<>();
        for (String s : strArr) {
            if (!s.contains("ad")) {
                list.add(s);
            }
        }
        
        return list.stream().toArray(String[]::new);
    }
}