• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Returning an String[] Array from a method

 
Jack Danyells
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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() ]));
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an alternative to Marilyn's suggestion, you could just return the List instead of returning an array.

Layne
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic