Java 将数组中出现的字符打印到二维数组

Java 将数组中出现的字符打印到二维数组,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,我在a-z的字符数组中有一些字符 如何验证输入数组中的字符是否介于a-z之间 打印2D数组中每个字符的匹配项,还打印输入数组中不存在的a-z之间的所有字符,匹配项计数为0。(Col1-字符,Col2-出现计数) 例如: Input - {'c','b','e','x','h'}; Output - Col1 Col2 a 0 b 1 c 1 d 0 等等,直到 我试图解决问题的第一部分,如下所示: public static boolean charVali

我在a-z的字符数组中有一些字符

  • 如何验证输入数组中的字符是否介于a-z之间
  • 打印2D数组中每个字符的匹配项,还打印输入数组中不存在的a-z之间的所有字符,匹配项计数为0。(Col1-字符,Col2-出现计数)
  • 例如:

    Input - {'c','b','e','x','h'};
    
    Output -
    
    Col1 Col2
    
    a    0
    
    b    1
    
    c    1
    
    d    0
    
    等等,直到

    我试图解决问题的第一部分,如下所示:

    public static boolean charValidation(char[] arr) {
    
           char[] charArr=new char[122];
    
            for(char c : arr)
            {
                int ascii = (int)c;
                charArr[ascii]=c;
            }
    
            for(int i=0;i<charArr.length;i++) {
                if(i<97 || i>122) {
                    if((int)charArr[i]!=0) {
                        return false;
                    }
                }
            }
            return true;        
        }
    
    公共静态布尔字符验证(char[]arr){
    char[]charArr=新字符[122];
    用于(字符c:arr)
    {
    int ascii=(int)c;
    字符[ascii]=c;
    }
    
    对于(inti=0;i,这里有一个简单的实现,它用字母表中的所有小写字母填充hashmap,并将它们的计数初始化为零

    public static boolean charValidation(char[] input) {
        boolean valid = true;
        String letters = "abcdefghijklmnopqrstuvwxyz";
        Map<Character, Integer> counts = new HashMap<>();
        for (char letter : letters.toCharArray()) {
            counts.put(letter, 0);
        }
        for (char letter : input) {
            if (letter < 'a' || letter > 'z') {
                valid = false;
            }
            else {
                int count = counts.get(letter);
                counts.put(letter, count+1);
            }
        }
    
        counts.entrySet().forEach(entry->{
            System.out.println(entry.getKey() + " " + entry.getValue());  
        });
    
        return valid;
    }
    
    public static void main(String args[]) {
        char[] input = {'c','b','e','x','h'};
        System.out.println("All inputs were valid: " + charValidation(input));
    }
    

    下面是一个简单的实现,它使用字母表中的所有小写字母填充hashmap,并将其计数初始化为零

    public static boolean charValidation(char[] input) {
        boolean valid = true;
        String letters = "abcdefghijklmnopqrstuvwxyz";
        Map<Character, Integer> counts = new HashMap<>();
        for (char letter : letters.toCharArray()) {
            counts.put(letter, 0);
        }
        for (char letter : input) {
            if (letter < 'a' || letter > 'z') {
                valid = false;
            }
            else {
                int count = counts.get(letter);
                counts.put(letter, count+1);
            }
        }
    
        counts.entrySet().forEach(entry->{
            System.out.println(entry.getKey() + " " + entry.getValue());  
        });
    
        return valid;
    }
    
    public static void main(String args[]) {
        char[] input = {'c','b','e','x','h'};
        System.out.println("All inputs were valid: " + charValidation(input));
    }
    

    您可以使用一个for循环来完成第一部分

    for (int i = 0; i < arr.length; i++)
    {
        int ascii = (int) arr[i];
    
        // if anyone of the ascii codes is outside this range...
        if (ascii < 97 || ascii > 122)
        { 
            // ... then the array has something other than a-z
            // ... so the array is not valid
             return false;
        }
    }
    
    // if we made it through the entire for loop,
    // this means we never came across an invalid char,
    // so return true 
    
    return true;
    
    for(int i=0;i122)
    { 
    //…那么数组中除了a-z之外还有其他东西
    //…因此数组无效
    返回false;
    }
    }
    //如果我们通过了整个for循环,
    //这意味着我们从未遇到过无效字符,
    //因此,返回真实
    返回true;
    
    您只需一个for循环即可完成第一部分

    for (int i = 0; i < arr.length; i++)
    {
        int ascii = (int) arr[i];
    
        // if anyone of the ascii codes is outside this range...
        if (ascii < 97 || ascii > 122)
        { 
            // ... then the array has something other than a-z
            // ... so the array is not valid
             return false;
        }
    }
    
    // if we made it through the entire for loop,
    // this means we never came across an invalid char,
    // so return true 
    
    return true;
    
    for(int i=0;i122)
    { 
    //…那么数组中除了a-z之外还有其他东西
    //…因此数组无效
    返回false;
    }
    }
    //如果我们通过了整个for循环,
    //这意味着我们从未遇到过无效字符,
    //因此,返回真实
    返回true;
    
    您可以在第一个循环本身中执行所需的逻辑,如下所示:

            for(char c : arr)
            {
               int ascii = (int)c;
               if(ascii < 97 || ascii > 122)
               {
                   return false; 
               }
               return true;
            }
    
    for(字符c:arr)
    {
    int ascii=(int)c;
    如果(ascii<97 | ascii>122)
    {
    返回false;
    }
    返回true;
    }
    
    您可以在第一个循环本身中执行所需的逻辑,如下所示:

            for(char c : arr)
            {
               int ascii = (int)c;
               if(ascii < 97 || ascii > 122)
               {
                   return false; 
               }
               return true;
            }
    
    for(字符c:arr)
    {
    int ascii=(int)c;
    如果(ascii<97 | ascii>122)
    {
    返回false;
    }
    返回true;
    }
    
    对于第一个问题,数组有多大?如果大小不是问题,我将使用regexp实现它

    替换不在a-z中的任何字符,如果剩余字符串长度为零,则表示所有字符都来自a-z

    private static boolean validateChar(char[] arr) {
        if (arr == null || arr.length == 0) {
            return false;
        }
        return new String(arr).replaceAll("[!a-z]", "").length() == 0;
    }
    

    对于第二个问题,我认为HashMap将比2d数组做得更好,除非您有真正需要2d数组的特定要求。

    对于第一个问题,数组有多大?如果大小不是问题,我将使用regexp来实现它

    替换不在a-z中的任何字符,如果剩余字符串长度为零,则表示所有字符都来自a-z

    private static boolean validateChar(char[] arr) {
        if (arr == null || arr.length == 0) {
            return false;
        }
        return new String(arr).replaceAll("[!a-z]", "").length() == 0;
    }
    
    对于第二个问题,我认为HashMap将比2d数组做得更好,除非您有真正需要2d数组的特定需求