• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

code

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;
public class Test1{
public static void main(String a[]){
Set s = new TreeSet();
s.add(new Person(20));
s.add(new Person(10));
System.out.println(s);
}
}
class Person{
Person(int i){}
}
****************************why above code is giving ClassCastException?
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
TreeSet sorts the elements according to the natural order of the elements.
But since the class 'Person' does not implement the 'Comparator' interface which is used to compare the elements, you are getting the class cast exception. I mean the person class is casted to the type 'Comparator'

Please correct me if im wrong.
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

i have compiled the program ...both options 1 and 4 are wrong ...i suppose there is something wrong with the answer.....

thanks
sri
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should get errors in both the following lines

c = b; // 1
s = c * b; //4

In first line a byte variable cannot be assigned to a char because
char cannot have negative values.

In the 4th line the result of any arithmetic operation is an int , but it's assigned to a shot and hence the error.
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Srinivasa Raghavan:
You should get errors in both the following lines

c = b; // 1
s = c * b; //4

In first line a byte variable cannot be assigned to a char because
char cannot have negative values.

In the 4th line the result of any arithmetic operation is an int , but it's assigned to a shot and hence the error.



Srinivasa, Are you sure you have answered the above mentioned Question only?

I dont find anything like
c = b; // 1
s = c * b; //4
in this code.

import java.util.*;
public class Test1{
public static void main(String a[]){
Set s = new TreeSet();
s.add(new Person(20));
s.add(new Person(10));
System.out.println(s);
}
}
class Person{
Person(int i){}
}
****************************why above code is giving ClassCastException?


 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is the question posted by Hema ?
She has posted another question in the same thread .. I replied to it .. Also Srikanth reddy replied to it .. Now what happened to the source ?? May be she has deleted..
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Srinivasa Raghavan:
Where is the question posted by Hema ?
She has posted another question in the same thread .. I replied to it .. Also Srikanth reddy replied to it .. Now what happened to the source ?? May be she has deleted..



No, you answered this question first, and then it seems like you answered another question as well in this thread
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand how s=c*b answer the question about the TreeSet...

So, concerning the TreeSet :

The ClassCastException is due to an attempt of casting a Person object to a Comparable object (not a Comparator).

A TreeSet maintains element sorted.
If the TreeSet is instanciated using a Comparator object (Using constructors : TreeSet(Comparator c) or TreeSet(SortedSet s)), this Comparator is used for comparing elements.

Otherwise, (instanciating the TreeSet with TreeSet() or TreeSet(Collection c)), elements must implements Comparable;

Look at Javadoc for the constructor TreeSet() :
Constructs a new, empty set, sorted according to the elements' natural order. All elements inserted into the set must implement the Comparable interface.
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sara Olsen:


No, you answered this question first, and then it seems like you answered another question as well in this thread



No .. There were two questions in same thread..
Here is the question .

 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by srikanth reddy:
hi,

i have compiled the program ...both options 1 and 4 are wrong ...i suppose there is something wrong with the answer.....

thanks
sri



The above was another reply to the question, even this is not related to TreeSet
[ October 17, 2005: Message edited by: Srinivasa Raghavan ]
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This seems to be a mysterious thread....

 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats why bartenders insist not to post different questions in the same Thread. This wastes time ...
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes..both were supposed to be in the same thread ....

might be hema has edited i suppose......

thanks
sri
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Srinivasa Raghavan:
Thats why bartenders insist not to post different questions in the same Thread. This wastes time ...



We do, we do, many many times - but who listens?

 
I guess I've been abducted by space aliens. So unprofessional. They tried to probe me with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic