1 回答

TA贡献1946条经验 获得超4个赞
您的任何Collections._____()调用都不会打印任何内容。它们只是对底层集合(listStrings)进行操作。因此,在每个步骤之后,您期望最终得到的结果如下:
//listStrings
Collections.sort(listStrings);
//listStrings sorted alphabetically, case sensitive
Collections.sort(listStrings, String.CASE_INSENSITIVE_ORDER);
//listStrings sorted alphabetically, case insensitive
Collections.sort(listStrings, Collections.reverseOrder());
//listStrings sorted alphabetically in reverse order, case insensitive
Collections.sort(listStrings, String.CASE_INSENSITIVE_ORDER);
//listStrings sorted alphabetically, case insensitive
Collections.reverse(listStrings);
//listStrings sorted alphabetically in reverse order, case insensitive
最后,在对 进行所有这些更改后listStrings,您尝试打印该集合。您在这里遇到的问题是,您实际上并没有刷新输出流,这可能是缓冲的。因此,不会打印任何内容。我重写了您的代码,使其具有完全相同的效果listStrings,并打印输出,如下所示:
public static void doIt(BufferedReader r, PrintWriter w) throws IOException
{
List<String> listStrings = new ArrayList<>();
String line;
while((line = r.readLine()) != null)
{
listStrings.add(line);
}
Collections.sort(listStrings, String.CASE_INSENSITIVE_ORDER.reversed());
for(String text : listStrings)
{
w.println(text);
}
w.flush();
}
我从我的 main 方法中调用它,如下所示:
public static void main(String[] args) throws Exception
{
doIt(new BufferedReader(new InputStreamReader(System.in)), new PrintWriter(System.out));
}
这是最终的效果:
输入:
ABCD
bcde
fegh
ijkl
输出:
ijkl
fegh
bcde
ABCD
添加回答
举报