단어가 공백 한 개 이상으로 구분되어 있는 문자열 my_string이 매개변수로 주어질 때, my_string에 나온 단어를 앞에서부터 순서대로 담은 문자열 배열을 return 하는 solution 함수를 작성해 주세요.
문자열 my_string을 split()으로 문자열 배열로 변환, 다시 Arrays.asList()로 List 객체로 변환한 다음 루프를 돌며 요소가 빈 문자열일 경우 remove()로 제거하는 과정을 거쳤다.
그 다음 반환 타입인 문자열 배열 형태로 변환하기 위해 toArray() 메서드를 사용했다.
import java.util.*;
class Solution {
public String[] solution(String my_string) {
List<String> list = new ArrayList<>(Arrays.asList(my_string.split(" ")));;
for(int i=0;i<list.size();i++){
if(list.get(i).equals("")){
list.remove(i);
i--;
}
}
String[] answer = list.toArray(new String[list.size()]);
return answer;
}
}
