Results 1 to 5 of 5
  1. #1
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default 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.
    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;
    	}
    Here is code thats calling it:
    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));
    	}
    Error:
    Java Code:
    Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList cannot be cast to javax.swing.ListModel
    so is there anyone who can help?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

    Default

    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.

  3. #3
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    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?

  4. #4
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    After a lot of searching and experimenting i came up with this:
    Java Code:
    public String[] accountStringConverter(int i)
    	{
    		Set<String> key = mapUserList.keySet();
    		String[] tempString = (String[])key.toArray(new String[key.size()]);
    
    		return tempString;
    	}
    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.

    Yet i don't understand it lol, i know its sad.

  5. #5
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    I'm posting because i was wondering if there is a person who can explain me how that piece of code works?
    There is nothing magical happening in the code.
    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

  1. Getting specific hash in a 2D-Array
    By benn22 in forum New To Java
    Replies: 0
    Last Post: 03-18-2011, 08:24 AM
  2. Changing a String array into a String?
    By BennyJass in forum New To Java
    Replies: 6
    Last Post: 01-16-2011, 03:42 PM
  3. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 10:01 PM
  4. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 12:41 PM
  5. Replies: 1
    Last Post: 08-03-2007, 11:52 AM

Posting Permissions

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