generics - How Collection copy function works in java -


i trying understand how java generic works. here problems.

  public static void main(string args[]){      list<object> obj = arrays.<object>aslist(23,"456",56.89);     list<integer> intb = new arraylist<>();     intb.add(234);     intb.add(345);     collections.copy(obj,intb);     for(object d : obj){         system.out.println(d);     }     }   

this work fine.but implemented own copy function

   public static <t> void copy(list<? super t> des,list<? extends t> scr){     for(t f : scr){         des.add(f);     }   } 

then used copy(obj,intb) instead of collections.copy(obj,intb). getting errors. don't understand why,but know "? extends t" means type subtype of t , "? super t" means type supertype of t. new generic in java, please bare me.

you cannot add fixed-length list:

list<object> obj = arrays.<object>aslist(23,"456",56.89); 

you can set new values elements (which collections.copy() does). see javadoc:

returns fixed-size list backed specified array.

what intended this:

list<object> obj = new arraylist<>(arrays.aslist(23,"456",56.89)); 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -