• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 231
1
  • 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!

-James
 
Sheriff
Posts: 22783
131
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:
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic