Ramprasad
Ramprasad

Reputation: 8071

What is the proper way to append an array inside another array in scala

I try to append an array element inside another array dynamically(by query result) as follows.But the element does not append in the array. What is the proper way to append an array inside another array?

var content:Array[Array[String]]=Array(Array())

content(0)=Array("h1","h2","h3","h4","h5","h6","h7","h8","h9")

val myResult = for(ts <- myQueryList) yield (
    content+:Array(e1,e2,e3,e4,e5,e6,e7,e8,e9)
)

Upvotes: 0

Views: 133

Answers (2)

Chirlo
Chirlo

Reputation: 6122

The problem is that the operation creates a new array that you need to reassign to content like this:

content=content+:Array(e1,e2,e3,e4,e5,e6,e7,e8,e9)

or a little bit cleaner:

content ++= Array(e1,e2,e3,e4,e5,e6,e7,e8,e9)

I hope this is what you want, it's not too clear what you want to do from your code.

Upvotes: 2

Nicolas Rinaudo
Nicolas Rinaudo

Reputation: 6168

Is there a particular reason you don't want to use Array.concat?

Something like:

myResult = Array.concat(content, myQueryList)

(Provided myQueryList is an Array[Array[String]]).

Upvotes: 0

Related Questions