Why does the method give a compile error in NetBeans
( error in question -- Type Mismatch Found Unit : required Array[Int] )
def createArray(n:Int):Array[Int] =
{
var x = new Array[Int](n)
for(i <- 0 to x.length-1)
x(i) = scala.util.Random.nextInt(n)
}
I know that if there was a if clause - and no else clause - then why we get the type mismatch.
However, I am unable to resolve this above error - unless I add this line
return x
The error is not happening because the compiler thinks what happens if n <= 0 I tried writing the function with n = 10 as hardcoded
Thoughts ?
for
in your function returnsUnit
, but return type isArray[Int]
. Hence the error. So just putx
as the last statement in the function. Check @Lee's answer below. – tuxdna