1 package test_19_1; 2 3 import java.util.Collections; 4 import java.util.Set; 5 import java.util.TreeSet; 6 7 public class SetTest { 8 9 public static void main(String[] args) { 10 Set<String> set = new TreeSet<String>(); 11 12 Collections.addAll(set, "Trying to create a method to count the vowels".split(" ")); 13 int total = 0; 14 for (String string : set) { 15 char[] chArr = string.toCharArray(); 16 int count = judgeChArr(chArr); 17 System.out.println(string + ": " + count); 18 total += count; 19 } 20 21 System.out.println("total : " + total); 22 23 } 24 25 private static int judgeChArr(char[] chArr) { 26 27 int count = 0; 28 for (char c : chArr) { 29 if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { 30 count++; 31 } 32 } 33 34 return count; 35 } 36 }
结果如下:
Trying: 1
a: 1
count: 2
creat: 2
method: 2
the: 1
to: 1
vowels: 2
total : 12