• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to return a string array of unknown size?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic