Reputation: 121
Output expected from program
List<String> alphabets = [ "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" ];
Upvotes: 2
Views: 3956
Reputation: 223
here is my single line answer
List.generate(26, (index) => print(String.fromCharCode(index+65)));
Upvotes: 10
Reputation: 318
You can use List.generate()
to generate a list of desired type.
The first parameter is the length of the desired list, which is the distance between Z to A character or simply 26. The second parameter is the function that map each index to your desired character.
var aCode = 'A'.codeUnitAt(0);
var zCode = 'Z'.codeUnitAt(0);
List<String> alphabets = List<String>.generate(
zCode - aCode + 1,
(index) => String.fromCharCode(aCode + index),
);
Take a look at the List.generate constructor of the List
class.
Upvotes: 8
Reputation: 121
The following program gives the desired output
void main() {
List<String> alphabets=[];
for(int i=65; i<=90; i++){
alphabets.add(String.fromCharCode(i));
}
print(alphabets);
}
About String.fromCharCode
read here
Upvotes: 5