Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Generic

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What did you try?
 
Sergio Tridente
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 44
1
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 44
1
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True, true

Comparable itself has a wild card class definition.

Cheers
Q
 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic