title: How to write a List of ArrayLists to CSV format in Java
body: <p>I have data in the below format:</p>
  
  <pre><code>   List&lt;ArrayList&lt;String&gt;&gt;
  </code></pre>
  
  <p>I want to write it to CSV. Below is my code:</p>
  
  <pre><code>private static void writeToCSV(List&lt;ArrayList&lt;String&gt;&gt; csvInput) throws IOException{
  
  String csv = "C:\\output.csv";
  CSVWriter writer = null;
  
  try {
      writer = new CSVWriter(new FileWriter(csv));
      } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
  
  for(ArrayList&lt;String&gt; each: csvInput){
      writer.writeNext(each);
  }
  
  
  writer.close();
  }//end of writeTOCSV
  </code></pre>
  
  <p>The method 'writeNext' allows only String[ ] as it's argument. When I try to type cast 'ArrayList each' into String[] using an Object[ ] as shown below, I am getting run time type casting error:</p>
  
  <pre><code>   Object[] eachTemp = each.toArray();
     writer.writeNext((String[]) eachTemp);
  </code></pre>
  
  <p>Could anyone please tell me where I am going wrong?</p>