【发布时间】:2026-01-30 03:45:02
【问题描述】:
我想按长度打印出三个最小的人,按最小的第一个排序,它需要在换行符上打印出每个人的姓名以及文本文件中以厘米为单位的最大长度。
到目前为止,我已经使用 Collections.sort 创建了一个比较器,以对我在文件中创建的数组进行排序,并对它们进行排序。
比较器:
Collections.sort(peopleFile,Comparator.comparingInt(People::getMaximumLength).reversed());
数组:
List<People> peopleFile = new ArrayList<>();
String[] tokenSize = fileRead.split(":");
String peopleName = tokenSize[0];
int maximumLength = Integer.parseInt(tokenSize[1]);
打印:
System.out.println("The three smallest people are: ");
peopleFile.stream().limit(3).forEach(System.out::println);
输出:
The three smallest people are:
David Lee, Length = 150 centimetres
Amber Jones, Length = 142 centimetres
Mandy Stones, Length = 152 centimetres
问题是它不输出最大的人,它只是在文本文件中打印出顺序。
我的输出应该是这样的:
The three smallest people are:
Amber Jones, Length = 142 centimetres
Samantha Lee, Length = 144 centimetres
Andre Bishop, Length = 145 centimetres
【问题讨论】:
-
您可以在*.com/questions/1946668/…找到更多相关信息
-
感谢您提供的信息