GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic" Watch "Generic" New topic
Author

Generic

sundar sankar
Ranch Hand

Joined: May 30, 2007
Posts: 42
Hi,
All i have created a gen class and a sub class gen i am getting a compile time error as Collections.sort(java.util.list<T> symbol not found,please help me.
Thanks
sankar
import java.util.*;
class gen<T>
{
private List<T> li=new ArrayList<T>();
public gen(List<T> l)
{
li=l;
}
public void so()
{
System.out.println(li);
Collections.sort(li);// problem in this line.
System.out.println(li);

}
}
class gen1
{
public static void main(String ssdf[])
{
List<Integer> li=new ArrayList<Integer>();
li.add(4);li.add(90);li.add(53);li.add(43);li.add(0);
gen<Integer> g=new gen<Integer>(li);
g.so();
System.out.println(li);
}
}
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Take a look at Collections.sort() method's signature:



Can you see what's wrong in your code now? If not, please post back and I'll try to explain.


SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
sundar sankar
Ranch Hand

Joined: May 30, 2007
Posts: 42
Hi Sergio Tridente and all
I tired in couple of ways but none of them came close to solve the problem.
please help me.
regards
sankar
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
What did you try?
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

OK. One hint:

The method's signature:
public static <T extends Comparable<? super T>> void sort(List<T> list)

is saying about argument 'list':
- It has to be a List
- of type T, being T any class that extends (implements) Comparable<? super T>. That is any class that is comparable to itself or superclass.

Some examples of the above in the API:
String implements Comparable<String>
Integer implements Comparable<Integer> (the same holds true for the other wrapper classes)
Date and Calendar implement Comparable<Date> and Comparable<Calendar> respectively.

Do you understand what is missing in the definition of your generic class (gen<T> now? Post back if you need more help.
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
"cannot find symbol" isn't really a helpful error-message.
eclipse says:
Bound mismatch: The generic method sort(List<T> of type Collections is not applicable for the arguments (List<T> . The inferred type T is not a valid substitute for the bounded parameter <T extends Comparable<? super T>>
Together with the above posts does that help?
Reet Goel
Greenhorn

Joined: May 02, 2007
Posts: 14
Hi,

I have a class called Basket with the signature
public class Basket<E>

Now if I say
Basket b1 = new Basket < Orange > ();

then what is the significance of parameterizing it with Orange type, as I can add any type to it and what it returns is of type Object.

It works exactly the same way,Basket b2=new Basket(); works.

am i right?
jinesh parikh
Ranch Hand

Joined: Apr 20, 2007
Posts: 48
I had tried according to what you have said Sergio
by implementing the comparable interfaceto the gen<T> class.
But still the error remains the samecould you please explain what is wrong?
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

It is T that has to implement Comparable, not gen<T>.

How did you do it? Please, post the modified version and I'll tell you what's wrong.
Quintin Stephenson
Ranch Hand

Joined: Nov 16, 2006
Posts: 40
Hello

The class definition gen<T> allows generics of T that are not comparable. Therefore, how is the compiler suppose to know that the list of type T is actually comparable and therefore allow Collections.sort() to perform its functionality. It doesn't and therefore will not compile.

The code will compile successfully by rewriting the class definition as:
class gen<T extends Comparable>
{...

Cheers
Q


If at first you don't succeed, try, try again. If you don't try you have failed.
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Originally posted by Quintin Stephenson:
The code will compile successfully by rewriting the class definition as:
class gen<T extends Comparable>
{...


That's right, but in fact it would be more correct to define it as:



or even better:

[ June 20, 2007: Message edited by: Sergio Tridente ]
Quintin Stephenson
Ranch Hand

Joined: Nov 16, 2006
Posts: 40
True, true

Comparable itself has a wild card class definition.

Cheers
Q
 
GeeCON Prague 2014
 
subject: Generic