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)
}
CodePudding user response:
If you take a closer look at that sequence of numbers: 4, 6, 7, 9, 10
You can see that between the first and second value there is a delta of 2. Between the second and third value there's a delta of 1. Then there's a delta of 2 again, and so forth.
You can create such a list with:
val list = MutableList(5)
{
index -> 4 index (index and 1) (index / 2)
}
Which will generate
4 0 (0 and 1) (0 / 2) == 4 0 0 0 == 4
4 1 (1 and 1) (1 / 2) == 4 1 1 0 == 6
4 2 (2 and 1) (2 / 2) == 4 2 0 1 == 7
... etc ...
CodePudding user response:
If you want a mutable list with just 4, 6, 7, 9, 10
in it, the most direct way would be to create it with mutableListOf
:
val list = mutableListOf(4, 6, 7, 9, 10)
This creates a size 5 MutableList
with 4, 6, 7, 9, 10 as its values. No real need to use the indexed lambda construct to create this unless you have some kind of calculation to make, or all elements are the same.
CodePudding user response:
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.