题解 | 【模板】序列操作
【模板】序列操作
https://www.nowcoder.com/practice/12da4185c0bb45918cfdc3072e544069
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
List<Integer> list = new ArrayList<>();
while (n-- > 0) {
int a = scan.nextInt();
if (a == 1) {
int num = scan.nextInt();
list.add(num);
} else if (a == 2 && !list.isEmpty()) {
list.remove(list.size() - 1);
} else if (a == 3) {
int num = scan.nextInt();
System.out.println(list.get(num));
} else if (a == 4) {
int index = scan.nextInt();
int num = scan.nextInt();
list.add(index+1, num);
} else if (a == 5) {
Collections.sort(list);
} else if (a == 6) {
list.sort(Collections.reverseOrder());
} else if (a == 7) {
System.out.println(list.size());
} else if (a == 8) {
List<String> list1 = new ArrayList<>();
for (Integer b : list) {
list1.add(String.valueOf(b));
}
System.out.println(String.join(" ", list1));
}
}
}
}

