Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Array problem in Scala

Tags:

string

scala

I just started playing in scala. I got a method that accepts string array as input

def Lambdatest(args:Array[String]) = args.foreach(arg=>println(arg))

And i have create a string array like this

var arr=new Array[String](3) 
arr(0)="ram"
arr(1)="sam"
arr(2)="kam"

When i call Lambdatest(arr), it throws an error like the below

scala> LambdaTest(arr)                       
<console>:7: error: not found: value LambdaTest
       LambdaTest(arr)
       ^

Whats the reason??

And is there a simple way to initialize the string arrays like the one in c#??

var strArr = new string[3] {"ram","sam","kam"};
like image 358
RameshVel Avatar asked Dec 12 '22 20:12

RameshVel


1 Answers

Your method definition and the invocation are not the same, you define Lambdatest yet invoke LambdaTest.

Additionally, you can define the array as:

val arr = Array("ram", "sam", "kam")

Your code will execute, providing you correct the method invocation:

scala> Lambdatest(arr)
ram
sam
kam
like image 78
gpampara Avatar answered Feb 07 '23 07:02

gpampara