简体   繁体   English

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

[英]Print occurrence of characters in an array to 2D array

I have some characters in char array from az.我在 az 的 char 数组中有一些字符。

  1. How can we validate that characters in input array is between az?我们如何验证输入数组中的字符是否在 az 之间?
  2. Print occurrence of every character in a 2D array and also print all the characters between az which are not present in the input array with occurrence count as 0. (Col1 - character, Col2 - occurrence count)打印 2D 数组中每个字符的出现次数,并打印 az 之间的所有字符,这些字符在输入数组中不存在,出现次数为 0。(Col1 - 字符,Col2 - 出现次数)

For example:例如:

Input - {'c','b','e','x','h'};

Output -

Col1 Col2

a    0

b    1

c    1

d    0

and so on till z...依此类推,直到 z...

I have tried to solve the 1st part of the question as below:我试图解决问题的第一部分,如下所示:

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;        
    }

It seems to be a hacky solution.这似乎是一个 hacky 解决方案。 Any optimised solution for 1st and 2nd?第一和第二的任何优化解决方案?

Here is a straightforward implementation which populates a hashmap with all lowercase letters of the alphabet, with their counts initialized to zero.这是一个简单的实现,它使用字母表的所有小写字母填充 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));
}

This prints:这打印:

a 0
b 1
c 1
d 0
e 1
f 0
g 0
h 1
i 0
j 0
k 0
l 0
m 0
n 0
o 0
p 0
q 0
r 0
s 0
t 0
u 0
v 0
w 0
x 1
y 0
z 0
All inputs were valid: true

You can do the first part with just one for loop.您可以只使用一个 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;

you can do the required logic in your first loop itself as follows:您可以在第一个循环本身中执行所需的逻辑,如下所示:

        for(char c : arr)
        {
           int ascii = (int)c;
           if(ascii < 97 || ascii > 122)
           {
               return false; 
           }
           return true;
        }

For your 1st questions, how large is your array?对于您的第一个问题,您的阵列有多大? If size is not an issue, I would use regexp to achieve it.如果大小不是问题,我会使用正则表达式来实现它。

Replace anything that is not in az and if the remaining string lengths zero, means all characters are from az.替换不在 az 中的任何内容,如果剩余的字符串长度为零,则表示所有字符都来自 az。

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

For your second question, I think HashMap will do a better job than 2d array, unless you have specific requirement that really needs a 2d array.对于您的第二个问题,我认为 HashMap 会比二维数组做得更好,除非您有真正需要二维数组的特定要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM