Concatenating arrays in Koltin

Simon Featherstone
3 min readMar 5, 2018
“How hard can it be?”

Adding the contents of one array to another would seem a very simple task. It Kotlin it’s a l̶i̶t̶t̶l̶e̶ ̶b̶i̶t̶ ̶m̶o̶r̶e̶ ̶c̶o̶m̶p̶l̶i̶c̶a̶t̶e̶d̶ quite easy…

TL;TR; val arrayc = arraya + arrayb plus I explore a lazy construction with no copy/re-allocation

In my case I wanted to add one additional element to the end of an array. This is what I found.

First up: The array size is immutable. This is the same as Java or C++ as memory is only allocated for the size the array is, so adding more requires a new array to be created(allocated), and the contents of the two arrays copied in. So lets try this with IntArray:

Here we are creating a new array with and using the Java function to copy the original arrays into the new array.

Next we use for loops to do the copying:

Next we are going to use the Array constructor to do the element copying

The lambda passed as the second parameter to the constructor fills the new array. The previous two examples could not use the Array<> because the constructor requires a lambda to construct the contents. But with this version I can now use Array<>

After publishing this post, Martin Saison from Prague pointed out that there is an even simpler way to achieve this in Kotlin. Just add the to arrays together to create a new array using + . This shown below in the same style, but in your code you would just use val array3 = array1 + array2

All of this will have complexity of O(n+m) and a size of O(n+m) because we are creating a new array, and coping element from both into the new array.

My next though is could I be a bit lazy about this. Could I do this through a delegate? The delegate would need to handle the get operator overloading, so it seemed this was the wrong approach, but I simple class could do it.

Here I am saving the two arrays into internal var’s. I then overload the size and get operator. I also provide a Iterator.

Size will not increase and construction complexity will be O(1)

Takeaways

Array<> and IntArray/ByteArray/ShortArray/DoubleArray/etc... all have similar interfaces, but share no inheritance and are also final. So my custom Array requires it’s own extension functions to be defined.

I should be looking at ArrayList<>which is more powerful, yet still has the continuous buffer which is super efficient with modern processors.

The simplest solution is also the most elegant. Kotlin strength is removing boiler plate code and complexity, so array1 + array2 is great solution where the cost is acceptable.

Edit’s
Thanks to Martin Saison for pointing out the very simple solution. We continue to learn from our peers!

Links

Gist Full Source

Research / Alternatives

About Me
I am a Kotlin/Java Android developer Contractor. I am always interested to hear about your project and what I can do to help each other. www.wedgetech.co.uk

--

--

Simon Featherstone

Passionate Android/Kotlin/Java Developer Contractor after 4 years at Microsoft/Skype and an age of C++ mostly in the broadcast sector. www.wedgetech.co.uk