import java.util.*; 7. public class AnonLam{ public static void main(Stringl args){ Stringl] direct = {"east", "west", "south","north"}; Sorters=new Sorter; for(String s2:direct){ System.out.println(s2+""); Arrays.sort(direct, s); System.out.println(); for(String s2:direct){ System.out.println(s2+" "); class Sorter implements Comparator<String>{ public int compare(String a, String b){ return b.compareTo(a) ;使用匿名内部类改写该程序
时间: 2024-03-30 16:38:13 浏览: 57
import java.util.*;
public class AnonLam {
public static void main(String[] args) {
String[] direct = {"east", "west", "south", "north"};
Comparator<String> sorter = new Comparator<String>() {
public int compare(String a, String b) {
return b.compareTo(a);
}
};
Arrays.sort(direct, sorter);
for (String s : direct) {
System.out.println(s + " ");
}
}
}
使用Lambda表达式改写该程序:
import java.util.*;
public class AnonLam {
public static void main(String[] args) {
String[] direct = {"east", "west", "south", "north"};
Comparator<String> sorter = (a, b) -> b.compareTo(a);
Arrays.sort(direct, sorter);
for (String s : direct) {
System.out.println(s + " ");
}
}
}
阅读全文