title: Creating a Generic CsvReader
body: <p>I'm trying to create a simple class to read a csv file and store the contents in an</p>
  
  <pre><code>ArrayList&lt;ArrayList&lt;T&gt;&gt;.  
  </code></pre>
  
  <p>I'm creating a generic class CsvReader so that I can handle data of different types: int, double, String.  If I had, say, a csv file of doubles, I was imagining I would use my class like this:</p>
  
  <pre><code>//possible method 1
  CsvReader&lt;Double&gt; reader = new CsvReader&lt;Double&gt;();
  ArrayList&lt;ArrayList&lt;Double&gt;&gt; contents = reader.getContents();
  
  //possible method 2
  CsvReader reader = new CsvReader(Double.class);
  ArrayList&lt;ArrayList&lt;Double&gt;&gt; contents = reader.getContents();
  </code></pre>
  
  <p>But method 1 won't work since type erasure prevents you from writing code like</p>
  
  <pre><code>rowArrayList.add(new T(columnStringValue)); 
  </code></pre>
  
  <p>But I can't even make the passing in a Double.class solution work.  The problem is that what's really going on is that I need my class "parameterized" (in the general sense of that word, not the technical java generics sense) on a type with the following property: it has a ctor accepting a single String argument.  That is, to create the row ArrayLists on, say, a Double csv file, I'd need to write:</p>
  
  <pre><code>StringTokenizer st = new StringTokenizer(line,",");
  ArrayList&lt;Double&gt; curRow = new ArrayList&lt;Double&gt;();
  while (st.hasMoreTokens()) {
   curRow.add(new Double(st.nextToken());
  }
  </code></pre>
  
  <p>Having passed in Double.class, I could get its String ctor using </p>
  
  <pre><code>  Constructor ctor = c.getConstructor(new Class[] {String.class});
  </code></pre>
  
  <p>but this has two problems.  Most importantly, this is a general constructor that will return a type Object, which I cannot then downcast into a Double.  Second, I would be missing "type" checking on the fact that I am requiring my passed in class to have a String arg constructor.  </p>
  
  <p>My question is: How can I properly implement this general purpose CsvReader?</p>
  
  <p>Thanks,
  Jonah</p>