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
添加回答
舉報