title: Making multiple objects in a for-loop
body: <p>I have code in Java that should make multiple objects within a for-loop, then add each object into an array. However, the code just copies the same (which is the last in the for-loop) object into each index <em>i</em> in the array once the loop ends.</p>
  
  <p>How can I correct this to have each separate object in its correct index in the array?</p>
  
  <p>I can post code if necessary, but it would be nice if someone could give me an example of how this would work.</p>
  
  <pre><code>for (int i = 0; i &lt; file.listFiles().length; i++) {
      if (fileList[i].isFile() &amp;&amp; !fileList[i].isHidden() &amp;&amp; fileList[i].getName().substring(fileList[i].getName().length() - 4).equalsIgnoreCase(".mp3")
              &amp;&amp; !fileList[i].equals(null)) {
          try {
              songs.add(new Song(fileList[i]));
              //System.out.println(songs[i].getTitle());
              //playlistInfo[i] = fileList[i].getName();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (TagException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Here is where I print some object info to the console. </p>
  
  <pre><code>System.out.println(getSong(1).getTitle());
  System.out.println(getSong(4).getTitle());
  </code></pre>