【问题标题】:arrayList of Strings contain specific characters java字符串的arrayList包含特定字符java
【发布时间】:2016-09-12 17:43:37
【问题描述】:

尝试一个 Java 学校 PigLatin 项目,我在其中循环遍历字符串的 Arraylist 并检查 char 0 处的字符串是否有元音。有没有比写出更简单的方法||对于每个元音(小写和大写)。这是我开始列出的一个例子......

    for (int i = 0; i<arrayList.size(); i++){
        if (arrayList.get(i).charAt(0) == 'a' || arrayList.get(i).charAt(0) == 'e'){
            System.out.println(arrayList.get(i) + "way");
        }

    }

我在网上找不到直接的答案。我见过 stringbuilder 和其他潜在的复杂选项,但不确定是否需要使用它们。

【问题讨论】:

    标签: java string arraylist character


    【解决方案1】:

    您可以创建一个静态方法isVowel(char c) 以在您的条件语句中使用:

    for (int i = 0; i<arrayList.size(); i++) {
        if (isVowel(arrayList.get(i).charAt(0))) {
            System.out.println(arrayList.get(i) + "way");
        }
    }
    
    public static boolean isVowel(char c) {
      return "AEIOUaeiou".indexOf(c) != -1;
    }
    

    【讨论】:

      【解决方案2】:

      如果它可以帮助您感觉更好,您可以使用开关盒,或者使用 ASCII 代码。

      定义一个函数 isVowel 或类似的东西,为自己处理相同的逻辑,并在需要时重用它。

      但是恕我直言,在最基本的层面上,您必须单独检查所有要检查的元音。

      【讨论】:

        【解决方案3】:

        您可以使用pattern matching 稍微减少混乱。如前所述,您还可以添加isVowel() 方法来分离元音检查逻辑。

        import java.util.Arrays;
        import java.util.List;
        
        public class PigLatin {
        
            public static void main(String[] args) {
        
                List<String> words = Arrays.asList(
                        "apricot",
                        "banana",
                        "lemon",
                        "orange",
                        "Input",
                        "Test",
                        "User"
                );
        
                for (int i = 0; i < words.size(); i++) {
                    String word = words.get(i);
        
                    if (startsWithVowel(word)) {
                        System.out.println(word + "way");
                    }
                }
            }
        
            private static boolean startsWithVowel(String word) {
                return word.matches("^[AEIOUaeiou].*$");
            }
        }
        

        您可以将索引 for 循环替换为 for-each loop

            for (String word : words) {
                if (startsWithVowel(word)) {
                    System.out.println(word + "way");
                }
            }
        

        这里是functional way,比较简洁,有兴趣的话:

            words.stream()
                 .filter(word -> startsWithVowel(word))
                 .forEach(word -> System.out.println(word + "way"));
        

        打印:

        apricotway
        orangeway
        Inputway
        Userway
        

        【讨论】:

          【解决方案4】:

          这是一个小代码 sn-p 给你一个更短的方法:)

          public static void main(String[] args) {
              String[] strings={"Aello","Bello","iello","Oello","Zello"};
              List<String> al=Arrays.asList(strings); //creating a list with entities....Replace it with your ArrayList...
              String vowelsString="aeiouAEIOU"; 
              for(String s:al){  //for each String in List
                  if(vowelsString.contains(s.charAt(0)+"")) //charAt(0) represents the beginning of string
                      System.out.println(s+" way");
              }
          

          【讨论】:

            猜你喜欢
            • 2014-02-02
            • 2016-01-17
            • 2017-08-29
            • 2016-05-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-12-25
            相关资源
            最近更新 更多