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

196
Visualizações
Is it possible to set a range of array values in Kotlin?

Let's say I have an array like this:

val grid = Array(10) { IntArray(10) { 1 } }

And now I want to set some of them to 2 instead. I could do this:

for (i in 0..5) {
    for (j in 0..5) {
        grid[i][j] = 2
    }
}

But what I'd like to do is this:

grid[0..5][0..5] = 2

Is there some faster way to do it like this?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can achieve something similar, e.g.:

grid[3..5, 2..4] = 5

by using the following extension function:

operator fun Array<IntArray>.set(outerIndices : IntRange, innerIndices : IntRange, newValue : Int) {
    for (i in outerIndices) {
        for (j in innerIndices) {
            this[i][j] = newValue
        }
    }
}

If you really wanted something like grid[3..5][2..4] = 5 you would then first need to implement a getter for the first IntRange which may hold a ~builder with the actual array reference so that the actual array can be adjusted on the set-call.

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