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 get uppercase letters in addition to lower case letters, you could also do the following:
String alphabetWithUpper = "abcdefghijklmnopqrstuvwxyz" + "abcdefghijklmnopqrstuvwxyz".toUpperCase();
char[] letters = alphabetWithUpper.toCharArray();
If you are using Java 8
char[] charArray = IntStream.rangeClosed('A', 'Z')
.mapToObj(c -> "" + (char) c).collect(Collectors.joining()).toCharArray();
with io.vavr
public static char[] alphanumericAlphabet() {
return CharSeq
.rangeClosed('0','9')
.appendAll(CharSeq.rangeClosed('a','z'))
.appendAll(CharSeq.rangeClosed('A','Z'))
.toCharArray();
}
import java.util.*;
public class Experiments{
List uptoChar(int i){
char c='a';
List list = new LinkedList();
for(;;) {
list.add(c);
if(list.size()==i){
break;
}
c++;
}
return list;
}
public static void main (String [] args) {
Experiments experiments = new Experiments();
System.out.println(experiments.uptoChar(26));
}
This is a fun Unicode solution.
char[] alpha = new char[26]
for(int i = 0; i < 26; i++){
alpha[i] = (char)(97 + i)
}
This generates a lower-cased version of alphabet, if you want upper-cased, you can replace '97' with '65'.
Hope this helps.