Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > TreeSet bug?

Reply
Thread Tools

TreeSet bug?

 
 
jacksu
Guest
Posts: n/a
 
      06-15-2006
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}));

 
Reply With Quote
 
 
 
 
Googmeister
Guest
Posts: n/a
 
      06-15-2006

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.

 
Reply With Quote
 
 
 
 
jacksu
Guest
Posts: n/a
 
      06-15-2006
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HashSet and TreeSet Ye Dafeng Java 4 11-16-2006 03:00 AM
Re: correct use of TreeSet Stefan Ram Java 2 02-26-2006 03:47 PM
Why would an add() in a TreeSet subclass fail? Rhino Java 17 02-24-2005 05:09 AM
TreeSet size() Problem Rhino Java 2 02-22-2005 04:49 PM
Re: JList customized with TreeSet Sandip Chitale Java 0 08-23-2003 09:44 PM



Advertisments