Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way to generate array of all letters in the alphabet

Tags:

java

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'

like image 202
Stupid.Fat.Cat Avatar asked Jul 10 '13 16:07

Stupid.Fat.Cat


People also ask

How do you create an array of alphabets in C++?

void createMine(int i); string alphabet[26] = { "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z" };

How do you turn an array of letters into a string?

char[] arr = { 'p', 'q', 'r', 's' }; The method valueOf() will convert the entire array into a string. String str = String. valueOf(arr);


1 Answers

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

char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray(); 
like image 194
Hunter McMillen Avatar answered Sep 20 '22 22:09

Hunter McMillen