Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
Initialize list with variable number of the same value

Is there a way to initialize a list x times with the value of y?

Thus instead of:

mutableListOf(0, 0, 0, 0)

something like:

makeMutableList(4, 0)
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Yes, just like this:

MutableList(4) { 0 }

This calls the factory function MutableList which takes a size and a lambda, passing to the lambda the index and setting the appropriate element to the result of the lambda. Thus, to create the list [1, 2, 3, 4] you can do this:

MutableList(4) { it + 1 }

If you don't need a mutable list, there is also a List factory function.

When running on the JVM, both List and MutableList functions create a java.util.ArrayList, though you shouldn't rely on the exact type.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!