5

I have a WrappedArray value of type Any. How do I convert to Array.

For Ex:

val flds = valArr(0)(2)
flds: Any = WrappedArray(F1,F2,F3,F4,F5)

I tried converting flds to String Array..

val flds = valArr(0)(2).toArray

But I got the below error

<console>:41: error: value toArray is not a member of Any

How do I convert..? I just want to loop through wrappedArray, But I didnt know how to do that.. Thats why I am trying to convert it to Array and loop through it.

1
  • WrappedArray should behave like an Array so you can loop as you generally do in an Array. Commented Oct 15, 2017 at 15:22

3 Answers 3

5

This is how to get WrappedArray from Array and vice versa:

val warr: WrappedArray[Int] = WrappedArray.make(Array(1, 2, 3))
val arr: Array[Int] = warr.array

or

val warr: WrappedArray[Int] = Array(1, 2, 3) // implicit conversion
val arr: Array[Int] = warr.array
Sign up to request clarification or add additional context in comments.

2 Comments

So how does this solve the problem when the value is an "Any". Spark's dataframe is returning Any=WrappedArray
@CarlZmola casting?
2

Starting with a WrappedArray[Any]

val warr: WrappedArray[Any] = WrappedArray.make(Array(1, 2, 3))

I can get to an Array[Int] this way:

val arr: Array[Int] = warr.toArray.map(_.asInstanceOf[Int])

Comments

1

First you need to tell Scala that your WrappedArray is a WrappedArray, so

import scala.collection.mutable.WrappedArray
val warr = flds.asInstanceOf[WrappedArray[T]]

where T is the type of the array contents. Afterwards you will have all the methods of WrappedArray available, e.g. warr.toArray.

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.