I need some help with the following problem: Create a method: public static int[
ID: 3616498 • Letter: I
Question
I need some help with the following problem:Create a method:
public static int[] getLetters (ArrayList<String> list)
Implement it by:
The method receives an array list of String objects and returns andarray of (primative) integers with the count of all letters in thearray list of strings. Each string in the array list can containand number of characters (alphabet or otherwise). The returnedarray of integers should always have 24 values, where each valuerepresents a letter: the first value(at index 0) is the number ofletters 'A', the second value at index 1 is 'B' and so on until'Z'.
Explanation / Answer
This just requires traversing the list of strings and lookingat every character one at a time. Here is a shortsolution.{ int[] count= new int[26]; for (Strings : list) //for all strings in the list for (char A : s.toCharArray())//for all chars in string s if (Character.isLetter(A) ) //if its aletter count[Character.toUpperCase(A) - 'A' ]++; //make a note ofit
return count; }
The trick was to turn all letter to lowercase and subtract 'A',this turns the char into an int by subtracting their asciivalues. Examples: 'A' - 'A' = 0 'B' - 'A' = 1 'C' - 'A' = 2
I hope this helps =)