Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Returning an String[] Array from a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Returning an String[] Array from a method" Watch "Returning an String[] Array from a method" New topic
Author

Returning an String[] Array from a method

Jack Danyells
Greenhorn

Joined: May 27, 2005
Posts: 3
To anyone that can help, I would appreciate it.

I have the following code and need to return an array of numbers, but I just don't know what needs to be done in the method, and I have to use String[] because this result is passed to other methods....


Marilyn added code tags
[ May 27, 2005: Message edited by: Marilyn de Queiroz ]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Unless you know in advance how many "personId"s you have, and thus the size of the array you want to create, I think the best way is to create an ArrayList to hold them temporarily. Then change the List to a String array which you assign to allNumbers.

Something like this:

List ids = new ArrayList();
ids.add( personId );
// end of loop
allNumbers = (String[])( ids.toArray( new String[ ids.size() ]));


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
As an alternative to Marilyn's suggestion, you could just return the List instead of returning an array.

Layne


Java API Documentation
The Java Tutorial
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Returning an String[] Array from a method
 
Similar Threads
Can we use two quries using prepared statement in one method
Retrieve record from DB?
Problem with preapred Statement
preparedStatement with like where clause
Can we use two quries using prepared statement in one method