title: Write to file from ArrayList
body: <p>I'm trying to write an ArrayList to a file in java. This is what I do in <strong>main class</strong>:</p>
  
  <p>To write Strings into the ArrayList I do:</p>
  
  <pre><code>list.add(String);
  </code></pre>
  
  <p>Then, to write it to the file:</p>
  
  <pre><code>readWrite.writing(list);
  </code></pre>
  
  <p>list is: <code>List&lt;String&gt; list = new ArrayList&lt;String&gt;();</code></p>
  
  <p><code>readWrite</code> references to this class where I have defined the methods to read/write to a file:</p>
  
  <pre><code> public void writing(ArrayList listToWrite) throws IOException {
      fileOutPutStream = new FileOutputStream (file);
      write = new ObjectOutputStream (fileOutPutStream);
      for (int i=0; i&lt;=listToWrite.size(); i++){
          write.writeObject(listToWrite.get(i));
      }
      write.close();
  }
  </code></pre>
  
  <p>When trying it on the console, I'm getting this exception:</p>
  
  <pre><code>Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
      at java.util.ArrayList.rangeCheck(Unknown Source)
      at java.util.ArrayList.get(Unknown Source)
      at //*I GET REFERENCED TO THIS LINE IN THE CODE ABOVE:* **write.writeObject(listToWrite.get(i));**
  </code></pre>