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.
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.
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."
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.