Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,239 questions

56,142 answers

573 users

How to use a variadic (varargs) function that accept any number of parameters in Scala

3 Answers

0 votes
def printAll(strings: String*): Unit = {
  strings.foreach(println)
}

// You can call this function with any number of arguments:
printAll("rust", "c", "c++", "java", "go", "python")



   
/*
           
run:
     
rust
c
c++
java
go
python
       
*/

 



answered Sep 18, 2024 by avibootz
0 votes
def f(args: String*): Unit = {
  var i : Int = 0;

  for (arg <- args) {
    println("Arg value[" + i + "] = " + arg);
    i = i + 1;
  }
}

// You can call this function with any number of arguments:
f("rust", "c", "c++", "java", "go", "python")


   
/*
           
run:
     
Arg value[0] = rust
Arg value[1] = c
Arg value[2] = c++
Arg value[3] = java
Arg value[4] = go
Arg value[5] = python
       
*/

 



answered Sep 18, 2024 by avibootz
0 votes
def f(args: String*): Unit = {
  var i : Int = 0;

  for (arg <- args) {
    println("Arg value[" + i + "] = " + arg);
    i = i + 1;
  }
}


val arr = Array("rust", "c", "c++", "java", "go")

f(arr: _*)


   
/*
           
run:
     
Arg value[0] = rust
Arg value[1] = c
Arg value[2] = c++
Arg value[3] = java
Arg value[4] = go
       
*/

 



answered Sep 18, 2024 by avibootz
...