我需要按优先级的升序显示/列出txt文件的内容。那么,我应该为任务的优先级选择一个独立的输入,还是可以拼接输入行?
private static void show() {
String[] items = getData("task.txt");
if (items.length == 0) {
System.out.println("There are no pending tasks!");
} else {
for (int i = items.length - 1; i >=0; i--) {
System.out.printf("[%d] %s\n", i + 1, items[i]);
}
}我的getData看起来是这样的:
private static String[] getData(String file) {
ArrayList<String> dataList = new ArrayList<>();
Scanner s=null;
try {
s = new Scanner(new FileReader(file));
while (s.hasNextLine()){
dataList.add(s.nextLine());
}s.close();
} catch (Exception e) {
System.out.println("Problem to open \"task.txt\".");
} finally {
if (s != null) {
try {
s.close();
} catch (Exception e) {
}
}
}
String[] items = new String[dataList.size()];
for (int i = 0; i < items.length; i++) {
items[i] = dataList.get(i);
}
return items;
}输入:
我需要做的事
5给植物浇水
11清洁房屋产出:
5给植物浇水
我需要做的事
11清洁房
发布于 2021-12-24 21:56:40
您只需对ArrayList数据表进行排序:(我假设“优先级项”格式已经在其中)
dataList.sort((o1, o2) -> {
Integer priority1 = Integer.parseInt(o1.split(" ")[0]);
Integer priority2 = Integer.parseInt(o2.split(" ")[0]);
return priority1.compareTo(priority2);
});把这个放在尝试-接-最后-阻止之后。
https://stackoverflow.com/questions/70476631
复制相似问题