Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda