Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass Arrays as arguments to a function in Scala?

Tags:

scala

forgive if this is too basic of a question,I'm pretty new to scala and been stuck at this particularly.

What I'm trying to achieve is,to have a function which takes an array as arguments,like so :

 evenOdd(1,2,3,4,5,6);  //where evenOdd is my function

The function definition looks like :

def evenOdd(x : Array[Int] = new Array[Int](6)){

}

It throws an error that too many arguments for the function.How can I achieve passing multiple array integers as fixed size in the function ?

like image 300
Technologeek Avatar asked Sep 07 '17 03:09

Technologeek


Video Answer


1 Answers

Either pass an Array to evenOdd:

evenOdd(Array(1, 2, 3, 4, 5, 6))

or define evenOdd as:

def evenOdd(x: Int*) = {...}
like image 200
Ziyang Liu Avatar answered Oct 02 '22 02:10

Ziyang Liu