android - Java Arraylist.Using Generics and Wildcards to create a reusable method -
i creating reusable method returns array-list.
here's trying achieve.
public list<?> getreusable(string method) {      list<?> mresuablelist = new arraylist<>();      switch (method) {     case method_1:         mresuablelist.add(new typeone("typeone","typeoneid"))         break;     case method_2:         mresuablelist.add(new typetwo("typetwo","typetwoid",1234,true));         break;     default:         break;     }     return mresuablelist; }   please tell if can achieve this.
try :
public abstract class typebase {  }  public class typeone extends typebase {     public typeone(string val1, string val2) {         // todo auto-generated constructor stub     } }  public class typetwo extends typebase {     public typetwo(string val1, string val2, int val3, boolean val4) {         // todo auto-generated constructor stub     } }  public <t extends typebase> list<t> getreusable(int method) {     list<t> mresuablelist = new arraylist<t>();     switch (method) {     case 1:         mresuablelist.add((t) new typeone("typeone", "typeoneid"));         break;     case 2:         mresuablelist.add((t) new typetwo("typetwo", "typetwoid", 1234, true));         break;     default:         break;     }     return mresuablelist; }  public class usingclass{     public void callreusable(){         list<typeone> lstone = getreusable(1);         list<typetwo> lsttwo = getreusable(2);     } }      
Comments
Post a Comment