1

I tried to convert my map:

private var map=Map[String,Double]()

into 2D array, but i had trouble doing it. Can someone help me please or give me and example of how it's done? I also had trouble in creating the array, so if you can help me with that i'll be glad.

Thank you for your time, and have a good day! :)

1
  • 6
    What are the inputs and what are the outputs (can you provide example, how exactly array should be formated)? What have you tried? Commented Jan 12, 2013 at 17:28

1 Answer 1

8

The toArray method looks promising:

  res6: scala.collection.immutable.Map[java.lang.String,Double] = Map(a -> 1.1, b -> 2.2, c -> 3.0)

  scala> res6.toArray
  res7: Array[(java.lang.String, Double)] = Array((a,1.1), (b,2.2), (c,3.0))

And if you really want an Array instead of tuples:

  scala> res6.toArray.map(x => Array(x._1, x._2))
  res8: Array[Array[Any]] = Array(Array(a, 1.1), Array(b, 2.2), Array(c, 3.0))
Sign up to request clarification or add additional context in comments.

3 Comments

Alternative syntax to avoid using _1 and _2: res6.toArray.map{ case(a,b) => Array(a,b) }
@Nick or res6.toArray.map(x => x.productElements.toArray)
@om-nom-nom: actually productElements is deprecated and does not exist anymore in 2.10 (now it is called productIterator)

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.