import java.util.ArrayList; import java.util.List; public class HistoryCommand { // 用List存储所有历史命令 private final List commandHistory = new ArrayList<>(); // 添加命令 public void addCommand(String command) { commandHistory.add(command); } // 获取所有历史命令 public List getHistory() { // 可选:返回副本,防止外部修改内部状态 return new ArrayList<>(commandHistory); } // 打印历史命令 public void printHistory() { if (commandHistory.isEmpty()) { System.out.println("暂无历史命令"); return; } for (int i = 0; i < commandHistory.size(); i++) { System.out.printf("[%d] %s%n", i + 1, commandHistory.get(i)); } } }