Hi Pavan Kundagol,
I thank you for your suggestion. 
I exchange the (key, value) parameters in put() method and it works. i 
understand why it is a ClassCastException, because I did like that  for "m" 
object :
        
        Map m = new HashMap();
        m.put(new String("Bako"), 1);
        m.put(new String("Takumah"), 2);
        MyOwnClass house = new MyOwnClass();
        house.setHeight(7.5);
        m.put(house.getHeight(), 3);
        MyOwnClass truck = new MyOwnClass();
        truck.setHeight(6.7);
        m.put(truck.getHeight(), 4);
        m.put(new Integer(145), 5);

        Map tm = new TreeMap();
        tm.putAll(m); 
And I think there was a problem with the type Integer and String in the map. 
And now, it works well.
Thanks.
Bako

 



________________________________
From: pavan kumar <[email protected]>
To: [email protected]
Sent: Tuesday, April 21, 2009 3:17:06 AM
Subject: [java programming] Re: Lab 1016 : 3.1 build and run a simple 
application that uses HashMap

Hi,

    This is my first time using java passions...so something is wrong please 
teach me...
public class treemap {

    public static void main(String[] args) {
        Map m = new HashMap();
        m.put(4, "four");
        m.put(1, "one");
        m.put(3, "three");
        m.put(2, "two");
        printMap(m);
        Map tm = new TreeMap();
        tm.putAll(m);
        printMap(tm);
    }

    private static void printMap(Map m) {
        System.err.println("priniting map:"+m);
        for (Iterator it = m.values().iterator(); it.hasNext();) {
            System.err.println(it.next());
        }
        System.err.println("----------------");
    }
}
output:
priniting map:{1=one, 2=two, 3=three, 4=four}
one
two
three
four
----------------
priniting map:{1=one, 2=two, 3=three, 4=four}
one
two
three
four
----------------

so what is ur problem....
tm.putAll(m);  //what is  'm' object...


On Tue, Apr 21, 2009 at 2:34 AM, Bakoly Takumah <[email protected]> wrote:

Hi everyone,
In the lab 1016, 3.1 build and run a simple application that uses HashMap, and 
part 4 of this segment advise us to do our own exercise : please do the 
following .... display them in both unsorted and sorted order. 
I did the unsorted order and it goes well, but I have a problem with the 
unsorted order, I got an execption : java.lang.ClassCastException.
I use putAll() method like that : 

Map tm = new TreeMap();
        tm.putAll(m);  
And then display tm.

I have looked at the JavaDoc about this exception for putAll but I don't really 
understand  what it means :     
ClassCastException - if the class of a key or value in the specified map 
prevents it from being stored in this map
Please, help.
Thanks in advance.
Bako







      
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to