Search code examples
javaalphabet

Better way to generate array of all letters in the alphabet


Right now I'm doing

for (char c = 'a'; c <= 'z'; c++) {
    alphabet[c - 'a'] = c;
}

but is there a better way to do it? Similar to Scala's 'a' to 'z'


Solution

  • I think that this ends up a little cleaner, you don't have to deal with the subtraction and indexing:

    char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();