Results 1 to 5 of 5
Thread: Hash Map into String Array
- 04-08-2011, 11:20 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
Hash Map into String Array
I was wondering if you could help me work out a code that turns keys from a hash map into a string array. It needs to be a method of its own.
I need it as a string so i can put it in a JList that will display all the users. So far I did this but JList wont accept ArrayList so it doesn't work and i don't know any other way.
Here is code thats calling it:Java Code:public ArrayList<String> accountReader(int i) { ArrayList<String> accountHolder = new ArrayList<String>(); accountHolder.addAll(this.mapUserList.keySet()); System.out.println(accountHolder); return accountHolder; }
Error:Java Code:private void databaseLoad() { mapSize = dtb_List.mapUserList.size(); accountList.addAll(dtb_List.accountReader(mapSize)); usernameList = new JList((ListModel) accountList); usernameList.setSelectionMode((ListSelectionModel.SINGLE_SELECTION)); }
so is there anyone who can help?Java Code:Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList cannot be cast to javax.swing.ListModel
- 04-09-2011, 01:55 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,548
- Rep Power
- 11
usernameList = new JList((ListModel) accountList);
As the compiler tells you that cast won't fly: an ArrayList<String> IS-NOT-A ListModel. Java is very strict about this: the only things that ARE-A ListModel are things that "implement" ListModel. The ListModel API docs helpfully lists the builtin interfaces that do this.
Read the JList docs to see what constructors are available and change this line so that
(1) You construct an instance of ListModel (DefaultListModel or whatever) based on the array list and use that model to construct your jlist.
or (easier, maybe)
(2) Make an Object[] (or Vector<?>) out of the array list and use that to construct the JList instance.
- 04-09-2011, 02:32 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
i'm really not understanding this, can somebody put up sample code how i can get all the keys out of a hashmap into a string array or ArrayList?
- 04-09-2011, 06:57 AM #4
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
After a lot of searching and experimenting i came up with this:
I'm posting because i was wondering if there is a person who can explain me how that piece of code works? For my programs it works awesome. Was able to finish entire administrator because of it.Java Code:public String[] accountStringConverter(int i) { Set<String> key = mapUserList.keySet(); String[] tempString = (String[])key.toArray(new String[key.size()]); return tempString; }
Yet i don't understand it lol, i know its sad.
- 04-09-2011, 07:19 AM #5
Senior Member
- Join Date
- Nov 2010
- Location
- Delhi
- Posts
- 135
- Blog Entries
- 1
- Rep Power
- 0
There is nothing magical happening in the code.I'm posting because i was wondering if there is a person who can explain me how that piece of code works?
You used toArray api of Set , i.e <T> T[] toArray(T[] a).
This returns you an array of type T (which could be anything).
Then you are tycasting the returned array to an array of String.
Actually, here you are using the power of Generics unknowingly.
Similar Threads
-
Getting specific hash in a 2D-Array
By benn22 in forum New To JavaReplies: 0Last Post: 03-18-2011, 07:24 AM -
Changing a String array into a String?
By BennyJass in forum New To JavaReplies: 6Last Post: 01-16-2011, 02:42 PM -
create a 2d char array from a 1D string array
By jschmall12 in forum New To JavaReplies: 1Last Post: 04-27-2010, 09:01 PM -
Convert Char Array to String Array
By Mayur in forum New To JavaReplies: 8Last Post: 10-12-2009, 11:41 AM -
I can't seem to pass the value of a string variable into a string array
By mathias in forum Java AppletsReplies: 1Last Post: 08-03-2007, 10:52 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks