Adair
Adair

Reputation: 1855

How to create an Array[Any] containing integer values in scala?

Update: I had misunderstood an error message, so this question is invalid.

val inputIds = Array[Any](7, 1, 3)

is sufficient to create an Array[Any] in Scala.

===========

I'm trying to create an array that needs to be passed to a method that takes an Array[Any], but the values are numeric.

At first I tried just the normal way of creating an array:

val inputIds = Array(7, 1, 4)

and got the error:

Type mismatch, expected: Array[Any], actual: Array[Int]

(from the IntelliJ editor, I guess I haven't checked if it's correct)

Then I tried creating Integer values directly, e.g. by:

Integer.getInteger("7")

and passing those into the Array constructor, but I kept getting the same error.

Now I tried:

val inputIds: Array[Any] = Array[Any](7, 1, 4)

and it gave me:

Type mismatch, expected: Array[Any], actual: Array[Array[Int]]

As you can tell I'm going a bit spare, all I want is wrapper types and not primitives! I guess Scala is trying to optimize the array for iteration, but all I need is a tiny array I can pass to a var args taking arrays of mixed type.. Or maybe a better way of creating that vararg method?? Right now its type is:

Array[Any]*

========

Okay, so I got my original question resolved (though there's still a dispute in the comments over whether I correctly understood why the error was being caused). However, it didn't solve my problem, which is the following:

I am trying to transpose an array of arrays with different types (some are nested, but ultimately either Double or Ints) and can't figure out how to get the scala type system to do it. Here's the basic example:

val integerArray = Array(7, 1, 4)
val nestedIntegerArray = Array(
  Array(6, 10, 60),
  Array(5, 89, 57),
  Array(15, 3, 5)
)
val nestedDoubleArray = Array(
  Array(.13, .9, .8),
  Array(.2, .777, .57),
  Array(.15, .3, .5)
)

val brokenComputation = typeErrorExample(integerArray, nestedIntegerArray, nestedDoubleArray)

where the method being called is:

  private def typeErrorExample(arrays: Array[_ <: Any]*)={
    val arrayDataFrame = Array(arrays).transpose
  }

This gives the error:

No implicit view available from Seq[Array[_]] => Array[U].
[INFO]     val arrayDataFrame = Array(arrays).transpose

What's the right way to do this? Should I use a non-array data structure instead?

Hopefully having more of the code will help the experts understand what was causing my original error too. (inputIds was renamed to integerArray for consistency).

Upvotes: 0

Views: 1315

Answers (5)

Mahesh Chand
Mahesh Chand

Reputation: 3250

There are many answers which are correct. But why even need to do asInstanceOf. You can do it easily.

scala> val arr: Array[Any] = Array(1,2,3)
arr: Array[Any] = Array(1, 2, 3)

Simple, Work is done.

Upvotes: 0

jwvh
jwvh

Reputation: 51271

@Adair, this appears to arrange the input arrays into the Array[Array[Any]] type that you're looking for.

def transpose(arrays: Array[_]*) :Array[Array[_]] =
  arrays.indices.map(x => arrays.map(_(x)).toArray).toArray

Now here's a word of advice: DON'T DO IT.

  • It's unsafe. This will throw an exception if the number of arrays passed exceeds the dimensions of each array.
  • Type Any is not what you want. When a data element becomes type Any then you've lost type information and it's a royal pain trying to get it back.

Upvotes: 1

flavian
flavian

Reputation: 28511

scala> Array(1, 2, 3).map(_.asInstanceOf[Any])
res6: Array[Any] = Array(1, 2, 3)

Upvotes: 0

Adair
Adair

Reputation: 1855

Jason's answer works, so does doing what the compiler error suggests when I ran it outside of intelliJ and changing the datatype in the method signature:

arrays: Array[_ <: Any]*

Upvotes: 0

Jason
Jason

Reputation: 15931

Explicitly casting is not ideal, but should do the trick.

 Array(1, 2, 3).asInstanceOf[Array[Any]]

Upvotes: 1

Related Questions