title: How to make a dynamic list of Objects
body: <p>Suppose I have a CSV file as:</p>
  
  <pre><code>employees.csv
  
  ID,Name,Role,Salary
  1,Pankaj Kumar,CEO,"5,000USD"
  2,Lisa,Manager,500USD
  3,David,,1000USD
  </code></pre>
  
  <p>I can parse this to the list of Employee objects as:</p>
  
  <pre><code>Employee.java
  
  package com.journaldev.parser.csv;
  
  public class Employee {
  
      private String id;
      private String name;
      private String role;
      private String salary;
  
      public String getId() {
          return id;
      }
      public void setId(String id) {
          this.id = id;
      }
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
      public String getRole() {
          return role;
      }
      public void setRole(String role) {
          this.role = role;
      }
      public String getSalary() {
          return salary;
      }
      public void setSalary(String salary) {
          this.salary = salary;
      }
  
      @Override
      public String toString(){
          return "ID="+id+",Name="+name+",Role="+role+",Salary="+salary+"\n";
      }
  }
  </code></pre>
  
  <p>But what if I want to make my code so that it can process files with more or less columns, without knowing in advance what their names are:</p>
  
  <pre><code>employees.csv
  
  ID,Name,Role,Salary, dateJoined, sex
  1,Pankaj Kumar,CEO,"5,000USD",Jan 05 2014 12:04:01PM,MALE
  2,Lisa,Manager,500USD,Feb 11 2012 12:04:01PM, FEMALE
  3,David,,1000USD,Jan 02 2013 12:04:01PM, MALE
  </code></pre>
  
  <p>I would like to make this so that it doesn't matter the number of columns in file.
  So for each column in file, code creates another element in the list of objects.</p>
  
  <p>The rest of my code:</p>
  
  <pre><code>package openCSV_fileReader;
  import java.io.FileReader;
  import java.io.IOException;
  import java.io.StringWriter;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  
  import au.com.bytecode.opencsv.CSVReader;
  import au.com.bytecode.opencsv.CSVWriter;
  public class OpenCSV_fileReader {
  
       public static void main(String[] args) throws IOException {
  
              List&lt;Employee&gt; emps = parseCSVFileAsList();
              System.out.println("**********");
              writeCSVData(emps);
       }
          private static void writeCSVData(List&lt;Employee&gt; emps) throws IOException {
              StringWriter writer = new StringWriter();
              CSVWriter csvWriter = new CSVWriter(writer,'#');
              List&lt;String[]&gt; data  = toStringArray(emps);
              csvWriter.writeAll(data);
              csvWriter.close();
              System.out.println(writer);
          }
  
          private static List&lt;String[]&gt; toStringArray(List&lt;Employee&gt; emps) {
              List&lt;String[]&gt; records = new ArrayList&lt;String[]&gt;();
              //add header record
              records.add(new String[]{"ID","Name","Role","Salary"});
              Iterator&lt;Employee&gt; it = emps.iterator();
              while(it.hasNext()){
                  Employee emp = it.next();
                  records.add(new String[]{emp.getId(),emp.getName(),emp.getRole(),emp.getSalary()});
              }
              return records;
          }
  
          private static List&lt;Employee&gt; parseCSVFileAsList() throws IOException {
              CSVReader reader = new CSVReader(new FileReader("employees.csv"), ',');
  
              List&lt;Employee&gt; emps = new ArrayList&lt;Employee&gt;();
              //read all lines at once
              List&lt;String[]&gt; records = reader.readAll();
  
              Iterator&lt;String[]&gt; iterator = records.iterator();
              //skip header row
              iterator.next();
  
              while(iterator.hasNext()){
                  String[] record = iterator.next();
                  Employee emp = new Employee();
                  emp.setId(record[0]);
                  emp.setName(record[1]);
                  emp.setRole(record[2]);
                  emp.setSalary(record[3]);
                  emps.add(emp);
              }
  
              reader.close();
  
              System.out.println(emps);
              return emps;
          }     
  }
  </code></pre>
  
  <p>Is this doable?
  This is where I am having trouble finding a way of doing this.</p>