package IO;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.Collections;import java.util.Scanner;// 由用户录入字符串,当输入end时结束,把用户输入的字符串//按字典顺序倒叙遍历public class IOend {public static void main(String[] args) throws IOException {method1();method2();}//用Scanenr来实现private static void method2() {// 创建Sanner对象Scanner sc = new Scanner(System.in);// 创建集合ArrayList array = new ArrayList();while (true) {System.out.println("请输入字符串,输入end结束");String str = sc.nextLine();if ("end".equals(str)) {array.add(str);Collections.sort(array);Collections.reverse(array);break;} else {array.add(str);}}System.out.println(array);}// 用标准的输入流来实现private static void method1() throws IOException {ArrayList list = new ArrayList();BufferedReader bs = new BufferedReader(new InputStreamReader(System.in));System.out.println("请了录入字符串,输入end结束录入");String line = null;while ((line = bs.readLine()) != null) {if ("end".equals(line)) {list.add(line);break;} else {list.add(line);}}Collections.sort(list);Collections.reverse(list);for (String s : list) {System.out.print(s + "\t");}}}
java中Scanner和标准输入流的哪一种更高效呢
最新推荐文章于 2024-09-29 09:52:59 发布