26

I want to make one array from two arrays.

I tried to do use +:

var array1 = intArrayOf(1,2,3)
var array2 = intArrayOf(4,5,6)
var array3 = array1 + array2

But it is not working unfortunately... How can I combine them?

1
  • 1
    "It's not working" is a great reminder to make sure you've specified the problem sufficiently. You didn't tell us what result you got, or how it differed from what you wanted. It's not even clear what you wanted -- arrays can be combined in multiple ways. Commented Jun 3, 2023 at 0:56

3 Answers 3

35

Actually, your exact code works for me. Tried it on multiple Kotlin versions. You can find the operator fun IntArray.plus(elements: IntArray): IntArray function that's being used for this in the docs here, and its source here.

var array1 = intArrayOf(1, 2, 3)        // 1, 2, 3
var array2 = intArrayOf(4, 5, 6)        // 4, 5 ,6
var array3 = array1 + array2            // 1, 2, 3, 4, 5, 6

Are you perhaps looking to do something different, like add the elements one by one and create a new array of length 3? You can do that like this:

val array4 = array1.zip(array2, Int::plus).toTypedArray()    // 5, 7, 9

The extra toTypedArray call is necessary only if you actually need an array, otherwise you can use the List<Int> that the zip function returns.

Sign up to request clarification or add additional context in comments.

Comments

23

Using the spread operator:

var array3 = intArrayOf(*array1, *array2)

This could be especially useful when you need to add some custom elements between the arrays, like intArrayOf(7, *array1, 8, 9, *array2, 10, 11).

Note spreading is much more efficient than plus-ing, because it creates only one resulting array. Using an equivalent plus version of the above spread example may use 5x more space and take 5x more time.

2 Comments

Would arrayOf() be as efficient if I added this to a recursive function with about 1k-5k recursions or should I better use List? The final array size is expected to grow to about 2000-3000 and will store class fields from reflection. It would be nice to hear if you ever had first-hand experience, if not I'll give it a go on test.
List is optimized for additions, it'll efficiently grow (not on every single add/addAll call). List is preferable to Array if you need to pass it around and accumulate (assuming you're not doing parallel recursion).
4

I am adding some other ways to merge two arrays.

var array1 = intArrayOf(1, 2, 3)
var array2 = intArrayOf(4, 5, 6)

Way 1:

var array3 = array1 + array2
print(array3.asList())

It will print [1, 2, 3, 4, 5, 6]. This one is given above.

You can replace the + by plus() method.

var array3 = array1.plus(array2)
print(array3.asList())

It will also work the same. When you are using +, at that time it actually calls the plus() method.

Way 2:

var array3 = array1.union(array2.asList())
print(array3)

It will print [1, 2, 3, 4, 5, 6].

union() method can take a collection as parameters. It can merge two arrays but it returns a Set. So, we will have unique data but it will merge two arrays.

Way 3:

var mergedArraySize = array1.size + array2.size
var mergedArray = IntArray(mergedArraySize)
var pos = 0;
for (value in array1) {
    mergedArray[pos] = value
    pos++
}
for (value in array2) {
    mergedArray[pos] = value
    pos++
}
print(mergedArray.asList())

It will also print [1, 2, 3, 4, 5, 6].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.