1

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.

2
  • 3
    > I know arrays are immutable in java Arrays are mutable, array size is not Commented Oct 2, 2013 at 6:07
  • "can I simply store all of the arguments passed to setArgs in an Array, instead of bothering with an ArrayList" I think you mean the other way around. Commented Oct 2, 2013 at 6:35

1 Answer 1

8

Yes, Using Arrays class asList() method

  List<String> argsList=  new ArrayList<String>(Arrays.asList(vals));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It says I need to wait 8 minutes to accept an answer (but it's 2AM now and I'm going to sleep). I'll close this in the morning =).

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.