- package NO_16;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- /*
- *可是按着题目上的答案,用比较器强行逆转排序,为什么我输出的结果没有倒序?
- * 是哪一步有问题?在线求指导。
- */
- public class LoopEnd2 {
- public static void main(String[] args) throws IOException {
- ArrayList<String> al = new ArrayList<>();
- stringSort(al);
- Comparator<String> com = Collections.reverseOrder();//获取反向的字符串比较器
- Collections.sort(al, com);//根据比较器排序集合
- for (String s : al) {
- System.out.print(s);
- }
- }
- public static void stringSort(ArrayList<String> al) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("输入字符串(输入'end'为结束字符串)");
- String line = null;
- while ((line = br.readLine()) != null) {
- if ("end".equals(line)) {
- break;
- } else {
- al.add(line);
- }
- }
- br.close();
- }
- }
复制代码 |