We help IT Professionals succeed at work.

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

Pradip Shenolkar
on
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

Comment
Watch Question

Full-stack Java Developer
Commented:
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

Author

Commented:
Ya , it works.
Thanks for quick reply.
Radek BaranowskiFull-stack Java Developer

Commented:
happy to help. cheers.