I want to create a MutableList
of alphabets form A
to Z
but so far I can only think of the following method as the shortest one without writing each and every alphabet.
fun main()
{
var alphabets: MutableList<Char> = mutableListOf()
for (a in 'A'..'Z')
{
alphabets.add(a)
}
print(alphabets)
}
So I wanted to know if there is any Lambda implementation or shorter method for the same?
Inside main() , create a variable called numbers of type List<Int> because this will contain a read-only list of integers. Create a new List using the Kotlin standard library function listOf() , and pass in the elements of the list as arguments separated by commas.
You can use CharRange()
or using ..
operator to create a range.
For example:
val alphabets = ('A'..'Z').toMutableList()
print(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]
or
val alphabets = CharRange('A','Z').toMutableList()
print(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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With