0

I have to concatenate k different lengths of strings into one string res and save res string into ArrayBuffer[String]().
But the k is variable.
For example,

val result = new ArrayBuffer[String]()
result.+=("1\t" + A.toString() + "\t" + ls.pid + "\t" + ls.did + "\t" + ls.sid + "\t" + ls.request_time.substring(0,10))

result.+=("2\t" + B.toString() + "\t" + ls.pid + "\t" + ls.did + "\t" + ls.sid + "\t")

result.+=("2\t" + B.toString() + "\t" + ls.pid + "\t" + ls.did + "\t")

result.+=("2\t" + B.toString() + "\t")

How to use a function with a variable-length argument to implement it?

Thanks in advance.

1 Answer 1

4

You can use the following syntax:

def f(args: String*) = {
    args.map{s =>
    //todo: process single item
    s
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I would just add the note that String* means is just a Seq[Sring] inside the method body so OP can do anything you can do on a sequence, like map or foldLef or foreach, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.