如题所述
import java.util.*;
/**
* @author hardneedl
*/
final public class StrDemo {
public static void main(String... args) {
Scanner scanner = new Scanner(System.in);
List<String> strList = new ArrayList<>(10);
String s = scanner.next();
while (!s.equals("!")) {
strList.add(s);
s = scanner.next();
}
Comparator<String> comparator = Comparator.comparingInt(String::length);
String max = strList.stream().max(comparator).orElse("?");
String min = strList.stream().min(comparator).orElse("??");
System.out.printf("最长字符串是:%s\r\n", max);
System.out.printf("最短字符串是:%s\r\n", min);
}
}
/**
* @author hardneedl
*/
final public class StrDemo {
public static void main(String... args) {
Scanner scanner = new Scanner(System.in);
List<String> strList = new ArrayList<>(10);
String s = scanner.next();
while (!s.equals("!")) {
strList.add(s);
s = scanner.next();
}
Comparator<String> comparator = Comparator.comparingInt(String::length);
String max = strList.stream().max(comparator).orElse("?");
String min = strList.stream().min(comparator).orElse("??");
System.out.printf("最长字符串是:%s\r\n", max);
System.out.printf("最短字符串是:%s\r\n", min);
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 2013-06-19
你可以循环数组A,把值都取出来,用个冒泡排序法,然后分别输出下标第一位和最后一位就可以了
第2个回答 2013-06-19
每个 一次比较一下,可以按照升序排列,也可以按照降序排列