Results 1 to 10 of 10
Thread: Properties to array
- 12-20-2009, 05:05 AM #1
[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 youLast edited by mac; 12-20-2009 at 04:14 PM.
- 12-20-2009, 05:31 AM #2gcampton Guest
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.
- 12-20-2009, 05:35 AM #3
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?
- 12-20-2009, 05:39 AM #4gcampton Guest
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.
-
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.
- 12-20-2009, 11:29 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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
- 12-20-2009, 04:19 PM #7
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();
}
-
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.
- 12-20-2009, 04:30 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 12-20-2009, 05:53 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Similar Threads
-
Properties
By Winarto in forum XMLReplies: 0Last Post: 08-29-2008, 04:32 PM -
properties help me please
By Winarto in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 08-29-2008, 01:42 PM -
Getting .mp3 properties
By Leprechaun in forum New To JavaReplies: 1Last Post: 02-06-2008, 06:55 AM
Bookmarks