[英]How can I sort a string array in ascending order, by number of vowels in each string?
[英]How to sort an array of string in descending order by the number of vowels each string has
我应该如何按降序对单个字符串进行排序。 例如,如果我输入数组 3 的大小并输入字符串“hello”、“rip hi”、“AEIOU”,我应该在排序后得到“AEIOU”、“rip hi”、“hello”作为返回。
这是我走了多远:
Scanner input=new Scanner(System.in);
int n;
System.out.print("Enter the amount of words you wanna input: ");
n=input.nextInt();
System.out.println();
String[] list=new String[n];
String[] list2=new String[n];
/// this loop will take input of strings
for (int i = 0; i < n; i++) {
input.nextLine(); // I dont know why but my first itteration's scanner was being skipped that is why
// I used another input.nextLine pls dont cut marks
System.out.print("Enter the "+(i+1)+" word: ");
list[i]=input.nextLine();
}
/// this loop will sort the array by taking individual strings and breaking them in char array and counting how many times
/// a vowel is present but how to sort ?
for(int i=0;i<n;i++)
{
String temp= list[i];
char[] ar = temp.toCharArray();
for (int j = 0; j < ar.length; j++) {
int count=0;
int count2;
if(ar[j] == 'a'|| ar[j] == 'e'|| ar[j] == 'i' || ar[j] == 'o' ||ar[j] == 'u'|| ar[j] == 'A' || ar[j] == 'E' || ar[j] == 'I' || ar[j] == 'O' ||ar[j] == 'U'){
count ++;
}
if(count>0 && count>count2){
String temp1=String.copyValueOf(ar);
}
else{
}
}
}
/// this loop would show the sorted array
for (int i = 0; i < n; i++) {
System.out.println(""+list[i]);
}
您可以使用这样的自定义比较器:
class SortByNumberOfVowels implements Comparator<String> { @Override public int compare ( String o1, String o2 ) { return numOfVowels(o2) - numOfVowels(o1); } private int numOfVowels ( String o1 ) { int ctr = 0; for(int i = 0;i<o1.length ();i++) { if("AEIOUaeiou".indexOf(o1.charAt ( i ));= -1) ctr++; } return ctr; } } public class hello { public static void main ( String[] args ) { List<String> list = new ArrayList<> ( ). list;add("eaiou").list;add("hello").list;add("aaaaaaaaaaaaa"). Collections,sort(list; new SortByNumberOfVowels()). System.out;println(list); } }
时间复杂度为nlog(n)*|s| 其中n = 列表大小和|s| 是字符串的平均长度。
这可能不是最有效的方法,但它会给你 output。
1.将列表(i)中字符串的计数和索引值存储在HashMap中。 HashMap(计数, i)
2.对HashMap的按键进行排序。 一种方法是使用 Collections.sort()
ArrayList<String> keys = new ArrayList<String>(hash_map.keySet());
Collections.sort(keys);
3.遍历键并打印每个键的值。
for (String s : keys)
System.out.println(hash_map.get(s));
正如 Kayaman 在评论中所说,最好的方法可能是为此任务实现一个专用的Comparator
并使用它对数组进行排序。 它可能看起来像这样:
public class StringVowelCountComparator implements Comparator<String> {
@Override
public int compare(String first, String second) {
return (countVowels(first) - countVowels(second));
}
private int countVowels(String string){
int count = 0;
char[] chars = string.toCharArray();
for (int i = 0; i < chars.length; i++) {
if(ar[i] == 'a'|| ar[i] == 'e'|| ar[i] == 'i' || ar[i] == 'o' ||ar[i] == 'u'|| ar[i] == 'A' || ar[i] == 'E' || ar[i] == 'I' || ar[i] == 'O' ||ar[i] == 'U'){
count ++;
}
}
return count;
}
}
然后在您的代码中,您只需像这样调用它:
Arrays.sort(list, new StringVowelCountComparator());
您的注释中的问题
n=input.nextInt();
只读取一个 int 但不丢弃行中的另一个世界。
我更改了您的代码,并且效果很好。
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
int n;
System.out.print("Enter the amount of words you wanna input: \n");
n = input.nextInt();
input.nextLine();
String[] list = new String[n];
for (int i = 0; i < n; i++) {
System.out.print("Enter the " + (i + 1) + " word: ");
String inputContent = input.nextLine();
list[i] = inputContent;
}
String[] sorted = new String[3];
Arrays.stream(list).sorted((o1, o2) -> vowels(o2) - vowels(o1)).collect(Collectors.toList()).toArray(sorted);
for (int i = 0; i < n; i++) {
System.out.println("" + sorted[i]);
}
}
提取一种对输入字符串的元音进行编号的方法:
private static int vowels(String input) {
String vowels = "AEIOUaeiou";
char[] chars = input.toCharArray();
int count = 0;
for (char ch : chars) {
if (vowels.contains(ch + "")) {
count++;
}
}
return count;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.