programmers

[JAVA] 배열원소의 길이

아잠만_ 2024. 3. 8. 21:37
문자열 배열 strlist가 매개변수로 주어집니다. strlist 각 원소의 길이를 담은 배열을 retrun하도록 solution 함수를 완성해주세요.

class Solution {
    public int[] solution(String[] strlist) {
        int[] answer = new int[strlist.length];
        for(int i=0;i<strlist.length;i++){
            answer[i]=strlist[i].length();
        }
        return answer;
    }
}

 

int [] answer = { }; 일 경우에는 answer의 배열 인덱스에 잘못하는 경우이기 때문에

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

라는 오류가 뜨기 때문에 꼭 배열 번지 수부터 구하도록 하자