Java: Instantiating a generic class with no default constructor -


i trying this:

public class basetable<t extends tableentry>  {      protected int mrows;     protected int mcols;     protected arraylist<t> mentries;      public basetable(int rows, int cols)     {         mrows = rows;         mcols = cols;         mentries = new arraylist<t>();         (int = 0; < rows; i++)         {             mentries.add(new t(cols)); //this obv. doesn't work         }     } } 

instantiating generics hard enough is, makes harder t here not have default constructor, takes single int parameter in constructor.

how can done?


i have asked follow question here too. i'd grateful if answer well.

this question related, relevant classes assumed have default constructor.

it said, can't create instance of t new, use factory pattern or prototype pattern

so constructor public basetable(int rows, int cols, linefactory factory) appropriate instance of factory.

in case, prefer prototype pattern, because tableentry objects light-weight. code like:

public basetable(int rows, int cols, t prototype) {          mrows = rows;   mcols = cols;   prototype.setcolumns(cols);   mentries = new arraylist<t>();   (int = 0; < rows; i++)   {     @suppresswarnings("unchecked")     t newclone = (t)prototype.clone();     mentries.add(newclone); //this obv. work :)   } }  public static void main(string[] args) {   new basetable<simpletableentry>(10, 2, new simpletableentry()); } 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -