• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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

Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!

Posts: 22701
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
I guess everyone has an angle. Fine, what do you want? Just know that you cannot have this tiny ad:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic