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

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

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 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