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 i in the array once the loop ends.
How can I correct this to have each separate object in its correct index in the array?
I can post code if necessary, but it would be nice if someone could give me an example of how this would work.
for (int i=0; i<file.listFiles().length; i++) {
if (fileList[i].isFile() && !fileList[i].isHidden() && fileList[i].getName().substring(fileList[i].getName().length()-4).equalsIgnoreCase(".mp3") && !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();
}
}
}
Thanks a ton!
Here is where I print some object info to the console.
System.out.println(getSong(1).getTitle());
System.out.println(getSong(4).getTitle());