🛫 Programmers School 중 배열 뒤집기 Java Coding 과정에서 발생한 오류
// code
class Solution {
public int[] solution(int[] num_list) {
int[] answer = new int[num_list.length];
int cnt = 0;
if (1<=num_list.length && num_list.length<=1000){
for (int i=num_list.length; i+1>0; i--){
if (0<=num_list[i] && num_list[i]<=1000){
answer[cnt] = num_list[i];
cnt++;
}
}
}
return answer;
}
}
// error message
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
Index 6 out of bounds for length 6
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[] answer = new int[num_list.length];
int cnt = 0;
if (1<=num_list.length && num_list.length<=1000){
for (int i=num_list.length-1; i+1>0; i--){
if (0<=num_list[i] && num_list[i]<=1000){
answer[cnt] = num_list[i];
cnt++;
}
}
}
return answer;
}
}