각각의 명령어마다 다른 동작을 해야함 -> 명령어가 여러개이므로 if문 보다는 switch가 가독성과 유지보수 측면에서 더 나을 것이라 판단.
parts[]는 크키가 1또는 2로 고정되어 있으므로 배열을 사용하고, 각각의 명령어에 따른 결과를 저장할 리스트는 동적으로 크기가 변해야하므로 ArrayList가 적절하다고 판단
나의 풀이
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int count = sc.nextInt();
sc.nextLine();
List<Integer> list = new ArrayList<>();
for(int i = 0; i < count; i++){
String line = sc.nextLine();
String[] parts = line.split(" ");
switch (parts[0]){
case "push_back":
int target = Integer.parseInt(parts[1]);
list.add(target);
break;
case "pop_back":
list.remove(list.size()-1);
break;
case "get":
int index = Integer.parseInt(parts[1]);
System.out.println(list.get(index-1)); //k번째 숫자를 출력
break;
case "size":
System.out.println(list.size());
break;
}
}
}
}