KevinS
KevinS

Reputation: 7875

How to create an array of arrays in scala

In Scala, I would like to create a TestNG DataProvider that converts all the elements of an enumeration into an Array of Arrays where each element in the outer array is an array containing one of the values of the enumeration.

This is my first attempt, but it returns a Set of Arrays.

@DataProvider(name = "profileIdProvider")
def provideProfiles() = {
  for (profile <- ProfileId.values) yield Array(profile)
}

What I need it to return is something like this:

Array(Array(value1), Array(value2))

Upvotes: 1

Views: 1023

Answers (2)

Chris Shain
Chris Shain

Reputation: 51339

Something like this should do (modified to use ProfileId.values of course):

def provideProfiles() = { 
    var returnVal = List[Array[Int]]()
    for (profile <- 1 to 5) returnVal :+= Array(profile)
    returnVal.toArray
}

Though I like @missingfaktor's answer more, of course.

Upvotes: 2

missingfaktor
missingfaktor

Reputation: 92056

@DataProvider(name = "profileIdProvider")
def provideProfiles() = {
  ProfileId.values.map(Array(_)).toArray
}

Not tested, but should work I think.

Upvotes: 4

Related Questions