Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

164
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda