gpt4 book ai didi

java - 接收比较器类类型

转载 作者:行者123 更新时间:2023-11-30 06:10:12 24 4
gpt4 key购买 nike

我有两个比较器。一个用于按首字母对单词进行排序(仅用于元音单词的排序)

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);
}
}

另一个用于按长度/vowelsCount 系数排序(用于排序类中的所有单词)

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;
}

和它们的父类(super class)

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 参数获取比较器类型?

最佳答案

您可以使用第三个参数来定义要使用的比较器。

public static void sort(String text, String regex, ComparatorType comp) {
// Code
Collections.sort(words, comp);
// Code
}

关于java - 接收比较器类类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36352251/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com