How does Java's generics work? -


could please me understand how java's generics work? understand concept of it. specific example of code don't understand compiler's error messages.

example code: test class

// test code  public class < listtype extends comparable < listtype >> { // make instance variable y public int y; // make instance variable s public string s;  //constructor method public a(int requiredy, string requireds) { y = requiredy; s = requireds; }  more code here... } 

then in different class wrote

list <a> = new arraylist<a>(); more code here... collections.sort(a) 

the error message getting

test.java:20: error: no suitable method found sort(list<a>) collections.sort(a);            ^ method collections.<t#1>sort(list<t#1>) not applicable   (inference variable t#1 has incompatible bounds     equality constraints:     upper bounds: comparable<? super t#1>) method collections.<t#2>sort(list<t#2>,comparator<? super t#2>) not applicable   (cannot infer type-variable(s) t#2     (actual , formal argument lists differ in length)) 

where t#1,t#2 type-variables:

t#1 extends comparable<? super t#1> declared in method <t#1>sort(list<t#1>) t#2 extends object declared in method <t#2>sort(list<t#2>,comparator<? super t#2>) 

i don't understand why compiler complaining type parameter. shouldn't collections work? because type parameters both mutually comparable.

either you're writing question wrong in order hide class names, or you're mistaken in representing generics.

if you're trying making class sorted, can implement comparable in class a others have suggested.

public class < listtype extends comparable < listtype >> { ... } 

the above code require class a accept class extends/implements comparable, , use listtype type erasure. since don't show how use listtype bound type, don't think want.

usually generics used bound type of parameter can use in class, in order provide type-safe operations in compile time.

import java.lang.override; public class <listtype extends comparable<listtype>>{     listtype lt;     a(listtype b){         this.lt = b;     }     static class b implements comparable<b>{         b(){};         @override         public int compareto(b b){             return 0;         }     }     static class c implements comparable<b>{         c(){};         @override         public int compareto(b c){             return 0;         }     }       public static void main(string[] args){         a<b> = new a<b>(new b());   //ok         a<c> _a = new a<c>(new c()); //error: not within bound         system.out.println("");     } } 

because class c not implementing comparable class itself, cannot pass class c variable class a constructor. if want create type accept classes extends comparable, use wildcard ?.

public class <listtype extends comparable<?>> 

or use single capital letter type better code styling

public class <t extends comparable<?>> 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -