[英]writing a method that takes an array of strings and orders the strings by number of vowels from least to greatest, and keeps the alphabetical order
該程序的目標是將給定的字符串數組更改為使該數組在按字母順序排列的同時,以最小的元音顯示最大的字符串。 輸出應如下所示:
[hello, Apple, hat, cat, Banana, AAA]
[cat, hat, Apple, hello, AAA, Banana]
我的代碼:
public static void main(String[] args) {
String[] strings = {"hello", "apple", "hat", "cat", "Banana", "AAA"};
System.out.println(Arrays.toString(strings));
orderByVowels(strings);
}
public static void orderByVowels(String[] order) {
for(int i = 0; i < order.length; i++) {
for(int j = i+1; j < order.length; j++) {
if(order[i].compareTo(order[j]) > 0) {
String tppp = order[i];
order[i] = order[j];
order[j] = tppp;
}
}
}
for (String order1 : order) {
System.out.print(order1 + " ");
}
}
我的問題是輸出以正確的順序放置字符串的順序,但未將其放置在數組中。 如何將新的字符串順序放入數組中並打印出來,就像上面顯示的輸出一樣?
我們首先需要編寫一種方法來計算字符串中的元音計數。 完成此操作后,我們需要在排序邏輯中同時使用元音計數和字符串比較。 下面的示例應該工作:
public static void main(String[] args) {
String[] strings = { "hello", "apple", "hat", "cat", "Banana", "AAA" };
// Displaying the initial array..
System.out.println(Arrays.toString(strings));
orderByVowels(strings);
System.out.println(Arrays.toString(strings));
}
public static void orderByVowels(String[] order) {
for (int i = 0; i < order.length; i++) {
for (int j = i + 1; j < order.length; j++) {
int countI = getVowelCount(order[i]);
int countJ = getVowelCount(order[j]);
if(countI > countJ
|| (countI == countJ && order[i].compareTo(order[j]) > 0)){
String tppp = order[i];
order[i] = order[j];
order[j] = tppp;
}
}
}
}
public static int getVowelCount(String input){
int count = 0;
for (int j = 0; j < input.length(); j++) {
char c =input.toLowerCase().charAt(j);
if(c=='a')
count++;
else if(c=='e')
count++;
else if(c=='i')
count++;
else if(c=='o')
count++;
else if(c=='u')
count++;
}
return count;
}
最近,我一直是番石榴Ordering
類的粉絲。 如果您不反對使用Lists和Google提供的庫,則可以通過Ordering.compound(secondaryComparator)
方法找到您想要的東西,該方法在第一個比較器出現平局的情況下使用secondaryComparator。
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import com.google.common.collect.Ordering;
import com.google.common.primitives.Ints;
public class SortStringArray {
public static void main(String[] args) {
final String[] testArray =
{"hello", "Apple", "hat", "cat", "Banana", "AAA"};
final List<String> testList = Arrays.asList(testArray);
final Ordering<String> byNumVowels = new Ordering<String>() {
@Override
public int compare(String left, String right) {
return Ints.compare(vowelCount(left), vowelCount(right));
}
private int vowelCount(final String str) {
if (str == null) {
return 0;
}
int count = 0;
final String strLowercase = str.toLowerCase();
for (int i = 0; i < strLowercase.length(); i++) {
final char c = strLowercase.charAt(i);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
count++;
}
}
return count;
}
};
Collections.sort(testList, byNumVowels.compound(Ordering.natural()));
System.out.print(testList);
}
}
魔術發生在這里:
Collections.sort(testList, byNumVowels.compound(Ordering.natural()));
首先檢查您定義的比較器,如果出現平局,則使用自然排序(字典順序)。
注意:如果給定的字符串為空,則Ordering.natural()返回的比較器將拋出空指針異常。 訂購還提供了一種易於閱讀的方式來處理此問題:
Collections.sort(testList, byNumVowels.nullsFirst().compound(Ordering.natural()));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.