You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
830 B

import java.util.ArrayList;
import java.util.List;
public class HistoryCommand {
// 用List<String>存储所有历史命令
private final List<String> commandHistory = new ArrayList<>();
// 添加命令
public void addCommand(String command) {
commandHistory.add(command);
}
// 获取所有历史命令
public List<String> 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));
}
}
}