Results 1 to 10 of 10
  1. #1
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Question [SOLVED] Properties to array

    Hi everyone,

    For the purposes of my program i need to make an array out of properties object. Can you please point me in the right direction here?

    This is what i put together but it does not work

    String[] schemas = null;
    for (int i =0; i < databaseConnectionStrings.keySet().toArray().lengt h; i++)
    {
    schemas[i] = databaseConnectionStrings.keySet().toArray()[i].toString();
    }

    Basically i need to take Keys stored in databaseConnectionStrings object and put them into an array.

    Thank you
    Last edited by mac; 12-20-2009 at 04:14 PM.

  2. #2
    gcampton Guest

    Default

    I think an object array would be better.

    Object[] schemas = new Object[0];

    eg if you have a class called Dog, and you want to create an array of all the different dogs in the world you can create:

    Dog[] dogList = new Dog[0];



    databaseConnectionStrings[] schemas = new databaseConnectionStrings[0];
    Last edited by gcampton; 12-20-2009 at 05:33 AM.

  3. #3
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    unfortunately that is not an option. I need a String array out of keys of that properties objects.

    Why does not code posted above work? At the end, i can sysout correct values and toString() makes element i am trying to add to an array a string as well. What am i missing here?

  4. #4
    gcampton Guest

    Default

    I'm unsure sorry, there's a couple typos in your above code: lengt h;

    try this:
    Java Code:
    String[] schemas = null; // need to increment array size, or just give it a value size.
    for (int i =0; i < databaseConnectionStrings.keySet().length; i++)
    {
    schemas[i] = databaseConnectionStrings.keySet().toString();
    }
    Last edited by gcampton; 12-20-2009 at 05:43 AM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm no pro in properties, so I can't speak for this portion of the code, but I do know that using arrays as noted above is trouble if you don't first construct the array, something like,
    Java Code:
    String[] schemas = new String[databaseConnectionStrings.keySet().length];
    for (.....)
    Last edited by Fubarable; 12-20-2009 at 06:49 AM.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Because a Properties object is a Hashtable and a Hashtable implements the Map interface you can get the keySet from it and Sets can be transformed to arrays: properties.keySet().toArray(); it builds an Object[] array for you. If you don't want that you have to pass another array to this (overloaded) method. Check the API documentation for those classes.

    kind regards,

    Jos

  7. #7
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Smile

    Quote Originally Posted by Fubarable View Post
    I'm no pro in properties, so I can't speak for this portion of the code, but I do know that using arrays as noted above is trouble if you don't first construct the array, something like,
    Java Code:
    String[] schemas = new String[databaseConnectionStrings.keySet().length];
    for (.....)
    Fubarable,gcampton thank you. This indeed was a solution. Very elegant, i might add. For some reason i was fixated on not allocating specific size to my array (did not want to go with say random 5). The fact that i actually know the size never occured to me. Again, thanks

    For those who may be wondering, this works great:


    String[] schemas = new String[databaseConnectionStrings.keySet().toArray().lengt h];

    for (int i = 0; i < databaseConnectionStrings.keySet().toArray().lengt h; i++) {
    schemas[i] = databaseConnectionStrings.keySet().toArray()[i].toString();
    }

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    There are few absolutes in programming, but one that is true and always will be true is this:

    any solution that JosAH posts will always be better than one of mine or gcampton. Unless we're talking about beer.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by mac View Post
    For those who may be wondering, this works great:


    String[] schemas = new String[databaseConnectionStrings.keySet().toArray().lengt h];
    Better not create an array for the single purpose of getting its length; better do this:

    Java Code:
    String[] schemas = new String[databaseConnectionStrings.keySet().size()];
    kind regards,

    Jos

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Fubarable View Post
    There are few absolutes in programming, but one that is true and always will be true is this:

    any solution that JosAH posts will always be better than one of mine or gcampton. Unless we're talking about beer.
    What's wrong with Grolsch?

    kind regards,

    Jos ;-)

Similar Threads

  1. Properties
    By Winarto in forum XML
    Replies: 0
    Last Post: 08-29-2008, 04:32 PM
  2. properties help me please
    By Winarto in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-29-2008, 01:42 PM
  3. Getting .mp3 properties
    By Leprechaun in forum New To Java
    Replies: 1
    Last Post: 02-06-2008, 06:55 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •