This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic

 
sundar sankar
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sundar sankar
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did you try?
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True, true

Comparable itself has a wild card class definition.

Cheers
Q
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic