AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Populating an Object[][]

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Wed Apr 19, 2006 9:12 pm    Post subject: Populating an Object[][] Reply with 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?

Thanks!
Back to top
Monique Y. Mudama
Guest





PostPosted: Wed Apr 19, 2006 9:12 pm    Post subject: Re: Populating an Object[][] Reply with quote



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





PostPosted: Wed Apr 19, 2006 9:12 pm    Post subject: Re: Populating an Object[][] Reply with quote



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





PostPosted: Thu Apr 20, 2006 3:12 am    Post subject: Re: Populating an Object[][] Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.