我有两个比较器。一个用于按首字母排序单词(仅用于元音单词的排序)
public class FirstLetterComparator extends ComparatorType {
@Override
public int compare(String o1, String o2) {
String upperObject1 = o1.toUpperCase();
String upperObject2 = o2.toUpperCase();
return upperObject1.charAt(0) - upperObject2.charAt(0);
}
}
另一个用length / vowelsCount coef排序(用于排序类中的所有单词)
public class VowelColComparator extends ComparatorType {
String vowelGroup = "AEIOUaeiou";
@Override
public int compare(String o1, String o2) {
int vCount1 = getVowelCount(o1);
int vCount2 = getVowelCount(o2);
float compareCoef1 = o1.length()/vCount1;
float compareCoef2 = o2.length()/vCount2;
return (int)(compareCoef1 - compareCoef2);
}
public int getVowelCount(String word){
int vowelCount = 0;
for (int i = 0; i < word.length(); i++){
char ch = word.charAt(i);
for (int j = 0; j < vowelGroup.length(); j++){
char v = vowelGroup.charAt(j);
if(ch == v)
vowelCount++;
}
}
return vowelCount;
}
他们的超类
public class ComparatorType implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
return 0;
}
}
在排序类中,我有两种类似的排序列表的方法
public class SortWords {
public static void sortVowelCol(String text, String regex){
Scanner scanner = new Scanner(text);
List<String> words = new ArrayList<>();
System.out.println();
System.out.println("Task1:");
while (scanner.hasNext()){
String word = scanner.next();
if(word.matches(regex)){
words.add(word);
}
}
Collections.sort(words, new VowelColComparator());
int lineCounter = 1;
System.out.println();
System.out.println();
System.out.println("Sorted Words:");
lineCounter = 1;
for(String w : words){
if(lineCounter == 12) {
System.out.print(w + "\n");
lineCounter = 0;
}
else
System.out.print(w + " ");
lineCounter++;
}
}
public static void sortVowelWords(String text, String regex) {
Scanner scanner = new Scanner(text);
List<String> vowelWords = new ArrayList<>();
System.out.println();
System.out.println("Task2: ");
while(scanner.hasNext()){
String word = scanner.next();
if(word.matches(regex)){
vowelWords.add(word);
}
}
Collections.sort(vowelWords, new FirstLetterComparator());
System.out.println();
System.out.println();
System.out.println("Sorted List:");
int lineCounter = 1;
for(String w : vowelWords){
if(lineCounter == 12) {
System.out.print(w + "\n");
lineCounter = 0;
}
else
System.out.print(w + " ");
lineCounter++;
}
}
}
主要课程
public class Main {
public static void main(String[] args) {
// write your code here
SingletonText.getInstance().parse();
SingletonText.getInstance().print();
SortWords.sortVowelWords(SingletonText.getInstance().getText().toString(), "^[AEIOUaeiou].*");
SortWords.sortVowelCol(SingletonText.getInstance().getText().toString(), "^[A-Za-z].*");
}
}
问题是如何在SortWords类中只使用一种方法而不是两种类似的方法?或者如何为Collections.sort参数获取比较器类型?
答案 0 :(得分:3)
您可以使用第三个参数来定义要使用的比较器。
public static void sort(String text, String regex, ComparatorType comp) {
// Code
Collections.sort(words, comp);
// Code
}
答案 1 :(得分:2)
你需要像这样重构两个函数:
public static void sortVowel(String text, String regex,Comparator comparator) {
Scanner scanner = new Scanner(text);
List<String> vowelWords = new ArrayList<>();
System.out.println();
System.out.println("Task2: ");
while(scanner.hasNext()){
String word = scanner.next();
if(word.matches(regex)){
vowelWords.add(word);
}
}
Collections.sort(vowelWords, comparator);
System.out.println();
System.out.println();
System.out.println("Sorted List:");
int lineCounter = 1;
for(String w : vowelWords){
if(lineCounter == 12) {
System.out.print(w + "\n");
lineCounter = 0;
}
else
System.out.print(w + " ");
lineCounter++;
}
}
}
答案 2 :(得分:0)
$(sectionDIV).find("#myImage").attr("src", imageFilePath);
然后我们可以像这样打电话
public static void sort(String text, String regex, String sortType) {
Collections.sort(words, sorttype.equals("Vowel") ? new VowelColComparator() : new FirstLetterComparator());
// Your code
}
答案 3 :(得分:0)
首先,当给定字符串中没有元音时,由于除零,您的 VowelColComparator.getVowelCount 可能会失败。在比较长度和元音数量的比率时,您可以执行以下操作:
float compareCoef1 = o1.length()/(vCount1+1);
float compareCoef2 = o2.length()/(vCount2+1);
您可以在 ComparatorType 类中使用工厂模式。即 ComparatorType 类将根据正则表达式决定使用哪个实例(比较器)。您可以根据需要添加任意数量的比较器。
public abstract class ComparatorType implements Comparator<String> {
final public String vowelFirstLetterRegex = "^[A-Za-z].*";
final public String vowelColRegex = "^[AEIOUaeiou].*]";
public static ComparatorType getInstance(String regex) {
if (regex.equals(vowelColRegex))
return new VowelColComparator();
else if(regex.equals(vowelFirstLetterRegex ))
return new FirstLetterComparator();
return null;
}
}
您的SortWords类将具有以下方法:
public static void sort(String text, String regex){
Scanner scanner = new Scanner(text);
List<String> words = new ArrayList<>();
System.out.println();
System.out.println("Task1:");
while (scanner.hasNext()){
String word = scanner.next();
if(word.matches(regex)){
words.add(word);
}
}
Collections.sort(words, ComparatorType.getInstance(regex));
int lineCounter = 1;
System.out.println();
System.out.println();
System.out.println("Sorted Words:");
lineCounter = 1;
for(String w : words){
if(lineCounter == 12) {
System.out.print(w + "\n");
lineCounter = 0;
}
else
System.out.print(w + " ");
lineCounter++;
}
}
答案 4 :(得分:0)
好吧,正如大家所说,你需要将Comparator<String>
作为第三个参数传递给你的方法:
sortVowelWords(String text, String regex, Comparator<String> cmp) {
//...
}
我想对比较器本身提出一些改进建议。使用lambda语法,可以更容易地编写它们:
static final Comparator<String> CMP_BY_FIRST_CHAR =
Comparator.comparing(s -> Character.toUpperCase(s.charAt(0)));
static final Comparator<String> CMP_BY_VOWEL_COEF =
Comparator.comparing(s -> 1f * s.length() / s.replaceAll("[^AEIOUaeiou]+", "").length());
在第二个比较器中,我向float
添加了显式强制转换以正确处理可能的无穷大值。