How to put commas in between arguments of a java function which accepts variable number of parameters ?

I have java class with one method.
The method accepts variable number of arguments.
My question : I need to pass the arguments at runtime. How to put commas in between arguments ?
 
public class Test {

	public void foo(String... args) {
	    for (String arg : args) {
	        System.out.println(arg);
	    }
	}

	public static void main(String args[])
	{
		Test t=new Test();
		t.foo("India");
		t.foo("US","India");
	}
}

Open in new window

Pradip ShenolkarAsked:
Who is Participating?
 
Radek BaranowskiFull-stack Java DeveloperCommented:
well you need to initialize an Array. for known and determined number of arguments it looks like that:

t.foo(new String[]{"India", "US"});

Open in new window

0
 
Pradip ShenolkarAuthor Commented:
Ya , it works.
Thanks for quick reply.
0
 
Radek BaranowskiFull-stack Java DeveloperCommented:
happy to help. cheers.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.