简体   繁体   中英

Is there a way to loop a char into an array?

To elaborate, what I mean is, if I wish to create an array of the alphabet:
(ie char[] alphabet = new char[26]; )
is it possible to use a for loop, for instance, to iterate over chars as opposed to me initializing each letter individually in brackets?
(ie char[] alphabet = {'a','b','c',...'z'}; )

Yes. Just add a value to a char in a loop. Like,

for (int i = 0; i < alphabet.length; i++) {
    alphabet[i] = (char) ('a' + i);
}

Alternatively, String.toCharArray() like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM