What is the meaning of daimond bracket in java generics containg two types List<car> -
What is the meaning of daimond bracket in java generics containg two types List<car<u>> -
whats list<car<u>>
?,what meaning of 2 parameters within list?am new generics , illustration code picked online...explain pls!
package generics; import java.util.arraylist; import java.util.list; class car<t>{ private t t; public t gett() { homecoming t; } public void sett(t t) { this.t = t; } } public class carlist{ public static <u> void addcar(u u, list<car<u>> cars){ //whats list<car<u>>?,what meaning of 2 parameters within list? car<u> car=new car<>(); car.sett(u); //could expalin above code cars.add(car); } public static void main(string[] args) { arraylist<car<string>> carlist=new arraylist<>(); carlist.addcar("audi", carlist); } }
first have @ auto class, allows generalize class , set/ of same type class. imagine (it maybe bad exmaple) want set cars engine. since different engines work in different way (gas, diesel, mule pulling car) create auto "start" want have interface later have concrete implementations of gas, diesel , mullet-engines:
interface engine{ public void start(); }
and first "implementation" of above interface:
class gasengine implements engine{ public void start(){ // check if enoth fuel available // start ignition // etc. etc. } }
since car-class defined generics first illustration interpreted this:
public class testme{ public static void main(string[] args) { // create "a car" without more info auto car = new car(); // since there no additional info can apply auto car.sett("anything"); car.sett(new integer(5)); // create auto "bound" engine-interface car<engine> cargeneral = new car<engine>(); // references implementing engine-interface can set auto gasengine mygasengine = new gasengine(); cargeneral.sett(mygasengine); // while wont compile: // cargeneral.sett("anything"); // note auto not engine!!! system.out.println("a auto engine, true of false? -> "+ (cargeneral instanceof engine)); } }
now @ point feared, illustration bad because have absolutly no idear on 'u' give code sence (i thought car-type first?...).
lets create minimal alter , method not called 'addcar' 'createcarwithengineandaddtocarlist'. in case this:
class carlist{ public static <u> void createcarwithengineandaddtocarlist(u u, list<car<u>> cars){ /* q: whats list<car<u>>?,what meaning of 2 parameters within list? * * a: "they define list cars able hold objects of type <car<u>>" * * a: @ point know: * * - cars of type list (has methods such 'add') * - cars contains objects of type car<u> * => no matter take out of list, of type car<u> * - type <u> not yet known , defined @ runtime */ // create new car-object car<u> car=new car<u>(); // set engine car.sett(u); // add together new auto list of existing cars cars.add(car); }
}
while hope have clarified "two parameters" bit more, above method still "bad". since 'know' our 'u' of type engine want have specified straight method. how utilize it:
public class testme{ public static void main(string[] args) { gasengine mygasengine = new gasengine(); arraylist<car<engine>> carlist = new arraylist<car<engine>>(); carlist.createcarwithengineandaddtocarlist(mygasengine, carlist); } }
brb. boss here
java generics
Comments
Post a Comment