TreeSet bug?

Discussion in 'Java' started by jacksu, Jun 15, 2006.

  1. jacksu

    jacksu Guest

    I got class cast exception for the following two lines of code.

    Not sure how treeset handle int[].

    Thanks.

    TreeSet<int[]> arr1 = new TreeSet<int[]>();
    arr1.add(new int[]{3, 2});
    System.out.println(arr1.contains(new int[]{2, 3}));
    System.out.println(arr1.contains(new int[]{3, 2}));
    jacksu, Jun 15, 2006
    #1
    1. Advertising

  2. jacksu

    Googmeister Guest

    jacksu wrote:
    > I got class cast exception for the following two lines of code.
    >
    > Not sure how treeset handle int[].
    >
    > Thanks.
    >
    > TreeSet<int[]> arr1 = new TreeSet<int[]>();
    > arr1.add(new int[]{3, 2});
    > System.out.println(arr1.contains(new int[]{2, 3}));
    > System.out.println(arr1.contains(new int[]{3, 2}));


    int[] does not implement the Comparable interface,
    so you cannot directly use it with TreeSet. You would
    need to create a customized Comparator or encapsulate
    the integer array in a class that implements the Comparable
    interface.
    Googmeister, Jun 15, 2006
    #2
    1. Advertising

  3. jacksu

    jacksu Guest

    Got it, thanks
    Googmeister wrote:
    > jacksu wrote:
    > > I got class cast exception for the following two lines of code.
    > >
    > > Not sure how treeset handle int[].
    > >
    > > Thanks.
    > >
    > > TreeSet<int[]> arr1 = new TreeSet<int[]>();
    > > arr1.add(new int[]{3, 2});
    > > System.out.println(arr1.contains(new int[]{2, 3}));
    > > System.out.println(arr1.contains(new int[]{3, 2}));

    >
    > int[] does not implement the Comparable interface,
    > so you cannot directly use it with TreeSet. You would
    > need to create a customized Comparator or encapsulate
    > the integer array in a class that implements the Comparable
    > interface.
    jacksu, Jun 15, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sandip Chitale

    Re: JList customized with TreeSet

    Sandip Chitale, Aug 23, 2003, in forum: Java
    Replies:
    0
    Views:
    643
    Sandip Chitale
    Aug 23, 2003
  2. Rhino

    TreeSet size() Problem

    Rhino, Feb 21, 2005, in forum: Java
    Replies:
    2
    Views:
    648
    Anton Spaans
    Feb 22, 2005
  3. Rhino
    Replies:
    17
    Views:
    1,013
    Rhino
    Feb 24, 2005
  4. Stefan Ram

    Re: correct use of TreeSet

    Stefan Ram, Feb 26, 2006, in forum: Java
    Replies:
    2
    Views:
    419
    Stefan Schulz
    Feb 26, 2006
  5. Ye Dafeng

    HashSet and TreeSet

    Ye Dafeng, Nov 15, 2006, in forum: Java
    Replies:
    4
    Views:
    1,645
    Mike Schilling
    Nov 16, 2006
Loading...

Share This Page