프로그래머스 알고리즘 문제 푸는 시점에서
요즘 배열 문제를 풀 때 빈번히 볼 수 있는 에러 ArrayIndexOutOfBoundsException 에러입니다.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at Solution.solution(Unknown Source)
at SolutionTest.lambda$main$0(Unknown Source)
at SolutionTest$SolutionRunner.run(Unknown Source)
at SolutionTest.main(Unknown Source)
class Solution {
public int[][] solution(int[] num_list, int n) {
int[][] answer = {};
int iIndexLength = num_list.length/n;
int index = 0;
for(int i= 0;i < iIndexLength ; i++){
for(int j=0 ; j < n;j++){
answer[i][j] = num_list[index];
index += 1;
}
}
return answer;
}
}
answer = new int[iIndexLength][n];
그래서
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
오류 발생
class Solution {
public int[][] solution(int[] num_list, int n) {
int[][] answer = {};
int iIndexLength = num_list.length/n;
int index = 0;
answer = new int[iIndexLength][n];
for(int i= 0;i < iIndexLength ; i++){
for(int j=0 ; j < n;j++){
answer[i][j] = num_list[index];
index += 1;
}
}
return answer;
}
}