aspose file tools
The moose likes Beginning Java and the fly likes How to return a string array of unknown size? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "How to return a string array of unknown size?" Watch "How to return a string array of unknown size?" New topic
Author

How to return a string array of unknown size?

D. McPherson
Greenhorn

Joined: Feb 04, 2010
Posts: 10
Hi everyone,

My first post here, I'm pretty new to Java and have what I'm sure is a stupid question to ask.

I have a procedure call (srvIMP.registerClient) within a procedure (clientMessenger) that passes an object and a string. Within registerClient these are added to a HashMap collection as a key/value pair. What I would like is for registerClient to then return an array of all the keys in the collection as an array of strings - allowing them to be displayed within a listbox. However, since I don't know how many values the returned array will contain - how can I initialise an array within clientMessenger to receive the returned array?

My code is as follows (I've removed a lot of stuff that isn't pertinent to my question to try to simplify things a little):



It is the line srvIMP.registerClient(this, strCompanyName) that is the problem - how can I initialise a string array e.g.

String[] myStringArray = new String[?];

when I don't know in advance how many strings are going to be returned?

Sorry if this is a ridiculously simple question, but I would be grateful for any help that you could provide.

Don.
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
Are you dead set on an array, because it seems to me if you don't know how big it is going to be you could use an ArrayList, which allows you to dynamically add elements and not worry about declaring the size.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

You don't have to know the size of the array which that method returns. It's already created, you just have to accept it:
D. McPherson
Greenhorn

Joined: Feb 04, 2010
Posts: 10
Thanks for the replies guys

An ArrayList is a possibility, I just hoped that I could avoid that step as an array could be used to directly populate the listbox.



I tried using the above, but got an error on compile of:

"incompatible types - found void but expected java.lang.String[]"

I'll have a go at using an ArrayList, and see how it goes.

Thanks again for the advice,

Don.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

D. McPherson wrote:

I tried using the above, but got an error on compile of:

"incompatible types - found void but expected java.lang.String[]"

Then the method doesn't return anything. Why did you ask how to get the array of strings which it returns, when it doesn't actually return anything?

Trying some other class isn't going to help. The method doesn't return anything, so trying to assign its value to an ArrayList won't work either. You need to go back a step and figure out why you thought getting its return value was something you should do.
D. McPherson
Greenhorn

Joined: Feb 04, 2010
Posts: 10
Doh!

Found the problem, in the original code I posted I declared the HashMap as shown below:



I then populated it:

i.e. Value and then Key

and of course the order of the parameters should be Key and then Value. The reason I wasn't getting any return values was that I was trying to add IClient objects into an array of strings

Once I got that sorted, everything else went fine and the code snippet that you posted Paul worked a treat. No wonder I couldn't get it to work before!

Cheers for your help guys,

Don.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to return a string array of unknown size?
 
Similar Threads
cyclic dependency
Blank JOptionPane RMI
Array of HashMaps
help with interface, static counter - RMI?
What is the "standard way" when getting data from a DB?