I'm trying to iterate an ArrayList and, during this loop, adding more elements to the list. The problem of my code is that isn't working and my first for loop cycles only one time (At first it dirs contains only the root directory).
for (ListIterator<File> iterator = dirs.listIterator(); iterator.hasNext(); ){
for (File file : iterator.next().listFiles()) {
if (checkCondition(file)) {
fileList.add(file);
}
if (file.isDirectory()) {
iterator.add(file);
}
}
}
Where dirs is an ArrayList of files.
I read a lot of answers on stackoverflow (in fact this is a one of them) but non of them seems to work (obviously I'm making some mistakes).