스택 s의 요소들을 pop하고 싶었음(3개의 요소 저장된 상태). 근데 인덱스 0의 요소가 출력되지 않는 거임! ㅠㅠ
for(int i = 0; i < s.length(); i++) {
System.out.print(s.pop()+" ");
}
public String pop() {
int l = length() - 1;
String popped = stack[l];
stack[l] = null;
return popped;
}
충격 사실 : length가 줄어든다 ㅋ
s.length가 3 -> 2 -> 1 이 되고 i는 0 -> 1 -> 2 가 되기 때문에 마지막 요소(인덱스 0)가 pop되지 않음 개쩐다
int l = s.length();
for(int i = 0; i < l; i++) {
System.out.print(s.pop()+" ");
}
오류 해결 ~~
'JAVA > 오류 해결' 카테고리의 다른 글
[JAVA] 인덱스 오버플로우 (0) | 2023.12.03 |
---|