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

508
Visualizações
Kotlin - Adding value to an empty array

I am learning kotlin concepts and had the following question when I was going through the topic Arrays.

I created an empty

val empty = arrayOf<Int>()
empty[0] = 2

So the above code will fail and cause ArrayIndexOutOfBoundsException which makes sense because the size of an array cannot be changed and since the array was initialized with no size it is treated as an empty array.

Here is where i thought this gets interesting

var emptyArray = arrayOf<Int>()
emptyArray += 1
emptyArray += 2

The above code doesn't fail and when i print the items I am getting the results back. I am assuming there is something going on when trying to add an element to an index versus adding it directly but I couldn't find any documentation that explains what is going on under the hood. Can someone please explain

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

0

This is because of how Kotlin's augmented assignment operators are overloaded.

For the assignment operations, for example a += b, the compiler performs the following steps:

  • If the function from the right column is available:
    • [...]
  • Otherwise, try to generate code for a = a + b (this includes a type check: the type of a + b must be a subtype of a).

"the function from the right column" refers to the right column of the table which I did not include in the quote, which are basically the operator functions plusAssign, minusAssign, timesAssign etc. Of course, these functions do not exist for arrays, so the second bullet point applies here.

To generate code for a = a + b, there needs to be a plus operator function for arrays, taking an element of that array, and returning an array, so that it is translated to a = a.plus(b). There is indeed such an operator function for Array<T>.

operator fun <T> Array<T>.plus(element: T): Array<T>

So to summarise:

emptyArray += 1

is translated into

emptyArray = emptyArray + 1

which is translated into:

emptyArray = emptyArray.plus(1)
over 4 years ago · Santiago Trujillo Relatório

0

In the second example, the + operator calls a function called plus, and then assigns the result back to the variable.

A look at the definition gives the following function:

/**
 * Returns an array containing all elements of the original array and then the given [element].
 */
public actual operator fun <T> Array<T>.plus(element: T): Array<T> {
    val index = size
    val result = java.util.Arrays.copyOf(this, index + 1)
    result[index] = element
    return result
}

As you see, a copy of the original is made but one larger. The new element is then assigned to the new, empty slot.


In the example a total of three arrays are allocated. First an empty, then a 1-size and finally a 2-size array, which is the final result. Each array still cannot change its size.

Note that you have to use var, and val will lead to a 'val cannot be reassigned' error.

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