wood burning stoves*
The moose likes Beginning Java and the fly likes How to create a method to accomodate array in method parameter? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to create a method to accomodate array in method parameter?" Watch "How to create a method to accomodate array in method parameter?" New topic
Author

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

James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 215
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

Joined: Oct 27, 2005
Posts: 19649
    
  18

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to create a method to accomodate array in method parameter?
 
Similar Threads
Why String Constructor exists in the API
System.arraycopy VS toArray()
JUnit. How to pass parameters into Test classes from suite() ?
Passing Array Elements to Method
jsp&java mail :How to send email to multiple users ?