Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to create a method to accomodate array in method parameter?

 
James Dekker
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,

I have a method that does this:



However, when that method is called there's a possibility that doSomeThingWithMultipleIds(String [] ids) method should take in an array [].

My question is how do I chain or facilitate the doSomeThingWithMultipleIds() to taken in an array?

Would it be something like this:



Right now I am passing in one id but will need to accommodate if it needs multiple ids...

So, how can I create two methods which do this:

(1) Create a method which one takes a single id and another that takes an array of ids.

(2) But the single one just calls the array related method? (Meaning, put a single id in an array, but pass array into the other method call)?

Happy programming!

-James
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's wrong with your code, except the creation of the String array? It's quite common for one method to wrap its arguments so they can be used in another method.

If you really dislike this, you can also check out var args (the section on Arbitrary Number of Arguments). In the end, var args can be treated as an array.

For a reference, this is how you should create your array:
Note that the first form is only possible if you initialize a newly declared variable. The following will not compile:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic