user1413969
user1413969

Reputation: 1301

Storing string arguments array in a list :java

Essentially, I want to have a method:

public void setArgs(String... vals)
{
    for (String s: vals)
        // Here I can add s every string passed to an array list

The thing I'm wondering is: can I simply store all of the arguments passed to setArgs in an Array, instead of bothering with an ArrayList? I know arrays are immutable in java, so I was wondering if you can somehow there's an easy way to extract the arguments without using a loop.

Thanks.

Upvotes: 1

Views: 546

Answers (1)

Suresh Atta
Suresh Atta

Reputation: 122008

Yes, Using Arrays class asList() method

  List<String> argsList=  new ArrayList<String>(Arrays.asList(vals));

Upvotes: 8

Related Questions