1

I can't seem to find a right solution. Trying to add a list of array to another array. With my Python background it is easily done but not in Kotlin.

val extra = arrayOf(7,7,7)

fun containerArray() {

    val even = arrayOf(2, 4, 6)

    val odd = arrayOf(1, 3, 5)

    val arr1 = arrayOf(even, odd)

    val arr2 = arrayOf(*arr1, extra)



    print(arr2.contentDeepToString())

 }

 fun main() {

     for (i in 1..3) {

        containerArray() 

    }

 }

When executing above code I receive...

[[2, 4, 6], [1, 3, 5], [7, 7, 7]][[2, 4, 6], [1, 3, 5], [7,7,7]][...
What I want to achieve is this ....

[ [2, 4, 6], [1, 3, 5], [7, 7, 7], [7, 7, 7], [7, 7, 7]]

3 Answers 3

3

I assume you want to get the following array:

[[2, 4, 6], [1, 3, 5], [7, 7, 7]]

There is an overridden + (plus) operator for Arrays in Kotlin, you can use it for adding arrays:

val arr2 = arr1 + extra

Resulting array arr2 will contain all elements of the original array arr1 and then all elements of the array extra.

ADDITIONALLY:

  • You can add another array to arr2:

    val anotherArray = arrayOf(5, 5, 5)
    val arr3 = arr2 + anotherArray
    // result: [[2, 4, 6], [1, 3, 5], [7, 7, 7], [5, 5, 5]]
    
  • If you want to use the same array arr2 to store elements, you can create it with var modifier:

    var arr2 = arr1 + extra
    arr2 += arrayOf(5, 5, 5)
    
  • There are different ways of what you are trying to achieve using a loop, for example:

    val even = arrayOf(2, 4, 6)
    val odd = arrayOf(1, 3, 5)
    val extra = arrayOf(7,7,7)
    
    var arr1 = arrayOf(even, odd)
    
    for (i in 1..3) {
        arr1 += extra
    }
    

arr1 will contain next elements: [[2, 4, 6], [1, 3, 5], [7, 7, 7], [7, 7, 7], [7, 7, 7]]

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

8 Comments

What result do you want to get and are getting using + operator? This should work and the result array should be [[2, 4, 6], [1, 3, 5], [7, 7, 7]]. I guess you wrongly described the result you want to get in your question then.
Sergey and @ cactustictacs absolutely right. The code example works but if I try to add and other array to arr2 using loop it just replacing the last element of the array. I have not tried the mutableList but I think it is the right approach in this case. So this is the result I looking for [[2, 4, 6], [1, 3, 5], [7, 7, 7]] and after my loop add an extra element to the list like [[2, 4, 6], [1, 3, 5], [7, 7, 7], [5,5,5]] and so for on.
@PSoll I've added some additional information to my answer. I don't quite understand how you try to add elements in the loop, it would be nice to see some example (you can edit your question).
find a code here: play.kotlinlang.org/…
so you are calling containerArray() three times, and it prints the same result every time. What are you trying to achieve?
|
2

To add a thing to an array of things, and produce a new array, use plusElement:

val arr2 = arr1.plusElement(extra)

This not only works for arrays, but also works for any Iterables, i.e. Lists, Sets etc.

Comments

2

Do you get why you're getting that result? arrayOf(items) creates a new array wrapping those items, so arrayOf(even, odd) is an array that contains two arrays. Then you create another array, containing that array-of-arrays and another single array. You're nesting them as you go

Sweeper's answer is probably what you want, but there are a lot of ways to combine collections, flatten sequences etc. Like one thing you can do is use the *operator (the "spread operator") to "unpack" your arrays, so you get the items instead:

   // unpack arr1 from an array of arrays, into just those arrays
   // equivalent to arrayOf([2, 4, 6], [1, 3, 5], extra)
   val arr2 = arrayOf(*arr1, extra)

   print(arr2.contentDeepToString())
   >> [[2, 4, 6], [1, 3, 5], [7, 7, 7]]

There's also flatMap, flatten etc - the best option depends on what you're doing!


Also when you say you want this:

[2, 4, 6], [1, 3, 5], [7, 7, 7]

that's just a bunch of values, not contained in anything, so we're assuming you want this:

[[2, 4, 6], [1, 3, 5], [7, 7, 7]]

where they're held in an array (or it could be a list). You could use the spread operator to unpack that into a bunch of values, but all you can do with that is pass it as a variable number of arguments to a function (which is what's happening in arrayOf)

3 Comments

Guys Thanks for your advise. Both the answers are added to my knowledge. # cactustictacs to your question yes I want to achieve a nested array [[2, 4, 6], [1, 3, 5], [7, 7, 7]] and than use and index to select a list of array. However in my scenario I'm using a for loop to add a new list to this array which does not work as I excpected.
@PSoll If you have your container array (the outer one) and the item arrays (the [2, 4, 6] etc), then you need to create your container, maybe starting with a couple of items included - arrayOf() or arrayOf(even, odd). Then just adding item arrays (using one of the plus methods in the other answers) is the easiest way to do it. arr1 += extra works!
@PSoll One thing to watch out for is that Kotlin encourages immutable collections, so there are lots of functions that don't change the original, they create a new copy with the changes, and return that. plusElement is one, so arr1.plusElement(extra) won't change arr1, it just produces a copy with the addition. That's necessary for arrays anyway - they're fixed length, if you want to "add" to one you need to create a new array and assign it to the variable. You'd generally use a mutableList instead - you can call toTypedArray() at the end if you really want an array for some reason

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.