kotlincollectionsmutablelist

MutableList(5) snippet in Kotlin


I have a problem with this snippet

fun main() {
    val list = MutableList(5)
    {
        4
    }
    println(list) 
}

this snippet will print [4, 4, 4, 4, 4]

and

fun main() {
    val list = MutableList(5)
    {
        index -> 4 + index
    }
    println(list) 
}

this snippet will print [4, 5, 6, 7, 8]

What i want to ask, and this is might be very very silly. If we using the same snippet as above, is there a way to make a custom list such as [4, 6, 7, 9, 10]

I try to use [4, 6, 7, 9, 10] or (4, 6, 7, 9, 10) or 4, 6, 7, 9, 10 or 4; 6; 7; 9; 10, it just doesn't work.

If I used 4; 6; 7; 9; 10, the list will print

[10, 10, 10, 10, 10]

if anyone care to help, this snippet below it just doesn't work

fun main() {
    val list = MutableList(5)
    {
        //[4, 6, 7, 9, 10]
        //(4, 6, 7, 9, 10)
        //4, 6, 7, 9, 10
        4; 6; 7; 9; 10
    }
    println(list) 
}

Solution

  • You've had a couple of answers telling you how to explicitly define a list with listOf/mutableListOf, and you've replied to them with a link (I can't access it) and asking about reading from standard input. I'm assuming that means your question hasn't been answered and you're trying to clarify what you're asking about, so I'll explain what's happening in the code in your replies, in case it helps clear things up.

    // on each line single numbers from 1 to 5
    val numbers = MutableList(5) { readln().toInt() } 
    println(numbers) // [1, 2, 3, 4, 5]
    

    MutableList(5) { //some function } is a list builder function (documentation here) - the first parameter is the size of the list you want to generate.

    The second parameter is the function that gets called to generate each item. It has a single parameter (as usual, called it unless you rename it) which contains the index of the item being generated. You can use this to generate values according to their position in the list, e.g. (it * 2) + 1 for a list of odd numbers.


    But you don't have to use that index value - you can produce a static value for each item, or pull data from anywhere you like to provide values. That's what readln() is doing - every time the function runs to generate one of the five items, it reads a line from standard input. You could also pull the next item from an Iterator, read a value from a Random generator, request something from an API - whatever you want. All that matters is that a value is returned, so the code in your function acts as an expression and returns a value to be used as the item in the list.

    So if you really wanted to, you could do this:

    // using an iterator because it has state, i.e. it remembers where it's up to in the list
    val stuff = listOf("1", "4", "6", "7", "2").iterator()
    
    fun main() {
       val myList = List(5) { stuff.next().toInt() }
       println(myList)
    }
    

    It's functionally the same as reading from standard input, it's just it polls the stuff iterator for values instead.