Skip to main content
Code Review

Return to Answer

added 1 character in body
Source Link
mtj
  • 4.9k
  • 11
  • 21

Hmm... characters in the alphabet held in an array of dimension 26. ToToo bad, for me as a German person your program will not work, as we have äöüß. Or in other terms: this is the age of unicode and internationalization, and characters as array indexes don't go well any more. (そして、アジア全土に...)

Thus: better use a dynamic structure which takes the characters (or interger code-points in unicode) as the key and the count as a value.

Then, once again (I feel I start to repeat myself quite a lot) what remains is a grouping operation, and this is already in the standard library. Thus: yes OK for an exercise, but have a look at streams and grouping. In this case, start out with this central element:

String s = ...
Map<Integer, Long> perCharacterCount = s.codePoints()
 .boxed()
 .collect(Collectors.groupingBy(
 Function.identity(),
 Collectors.counting()));

Hmm... characters in the alphabet held in an array of dimension 26. To bad, for me as a German person your program will not work, as we have äöüß. Or in other terms: this is the age of unicode and internationalization, and characters as array indexes don't go well any more. (そして、アジア全土に...)

Thus: better use a dynamic structure which takes the characters (or interger code-points in unicode) as the key and the count as a value.

Then, once again (I feel I start to repeat myself quite a lot) what remains is a grouping operation, and this is already in the standard library. Thus: yes OK for an exercise, but have a look at streams and grouping. In this case, start out with this central element:

String s = ...
Map<Integer, Long> perCharacterCount = s.codePoints()
 .boxed()
 .collect(Collectors.groupingBy(
 Function.identity(),
 Collectors.counting()));

Hmm... characters in the alphabet held in an array of dimension 26. Too bad, for me as a German person your program will not work, as we have äöüß. Or in other terms: this is the age of unicode and internationalization, and characters as array indexes don't go well any more. (そして、アジア全土に...)

Thus: better use a dynamic structure which takes the characters (or interger code-points in unicode) as the key and the count as a value.

Then, once again (I feel I start to repeat myself quite a lot) what remains is a grouping operation, and this is already in the standard library. Thus: yes OK for an exercise, but have a look at streams and grouping. In this case, start out with this central element:

String s = ...
Map<Integer, Long> perCharacterCount = s.codePoints()
 .boxed()
 .collect(Collectors.groupingBy(
 Function.identity(),
 Collectors.counting()));
Source Link
mtj
  • 4.9k
  • 11
  • 21

Hmm... characters in the alphabet held in an array of dimension 26. To bad, for me as a German person your program will not work, as we have äöüß. Or in other terms: this is the age of unicode and internationalization, and characters as array indexes don't go well any more. (そして、アジア全土に...)

Thus: better use a dynamic structure which takes the characters (or interger code-points in unicode) as the key and the count as a value.

Then, once again (I feel I start to repeat myself quite a lot) what remains is a grouping operation, and this is already in the standard library. Thus: yes OK for an exercise, but have a look at streams and grouping. In this case, start out with this central element:

String s = ...
Map<Integer, Long> perCharacterCount = s.codePoints()
 .boxed()
 .collect(Collectors.groupingBy(
 Function.identity(),
 Collectors.counting()));
lang-java

AltStyle によって変換されたページ (->オリジナル) /