 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Wed Apr 19, 2006 9:12 pm Post subject: Populating an Object[][] |
|
|
I have a HashMap and I need to fill a 2-dimential array, Object[][],
with the key-value pairs. I need to do this to supply a JTable with
the data from the HashMap.
I do not know what data is in the HashMap or what the key names are.
Can someone please show me how to do this?
Thanks! |
|
| Back to top |
|
 |
Monique Y. Mudama Guest
|
Posted: Wed Apr 19, 2006 9:12 pm Post subject: Re: Populating an Object[][] |
|
|
On 2006-04-19, MikeSmith813 (AT) gmail (DOT) com penned:
| Quote: | I have a HashMap and I need to fill a 2-dimential array, Object[][],
with the key-value pairs. I need to do this to supply a JTable with
the data from the HashMap.
I do not know what data is in the HashMap or what the key names are.
Can someone please show me how to do this?
|
It's not clear to me what you're finding difficult about this. Some
code and a "here's where I get stuck" indicator would be helpful.
I would iterate over the keySet(). That gives you each key, and with
the key you can use the get(Object key) method to get the value.
Does that help?
--
monique
Help us help you:
http://www.catb.org/~esr/faqs/smart-questions.html |
|
| Back to top |
|
 |
Eric Sosman Guest
|
Posted: Wed Apr 19, 2006 9:12 pm Post subject: Re: Populating an Object[][] |
|
|
MikeSmith813 (AT) gmail (DOT) com wrote On 04/19/06 16:37,:
| Quote: | I have a HashMap and I need to fill a 2-dimential array, Object[][],
with the key-value pairs. I need to do this to supply a JTable with
the data from the HashMap.
I do not know what data is in the HashMap or what the key names are.
Can someone please show me how to do this?
|
The most direct approach is probably to use the entrySet()
method on the Map. This method returns a Set whose elements
are Map.Entry objects, each of which holds a key and the
corresponding value. Iterate over the Set to visit all the
Map.Entry objects, and extract a key and a value from each
to populate your array.
--
Eric.Sosman (AT) sun (DOT) com |
|
| Back to top |
|
 |
Jubz Guest
|
Posted: Thu Apr 20, 2006 3:12 am Post subject: Re: Populating an Object[][] |
|
|
HashMap myMap; // Initialized elsewhere
Object[][] myArray = new Object[myMap.size()][2];
int row = 0;
for (Iterator i = myMap.keySet().iterator(); i.hasNext(); row++) {
Object key = i.next();
Object value = myMap.get (key);
myArray[row][0] = key;
myArray[row][1] = value;
}
Sort of old school, but it'll work. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|