You need to modify i again to make sure it covers the full sequence of the Array List or go last->first as shown in other answers.
for (int i = 0; i < result.size(); i++) {
if (result.get(i).split("\\s").length != maxLength) {
result.remove(i);
i--; // everything has been moved up in the arraylist
}
}
Also, ArrayList takes linear time to remove a single element, so repeated removal is a bad idea.
Either use a LinkedList, which can remove in constant time during iteration, or first collect the elements in a HashSet and then remove them at the end using Collection.removeAll(Collection).
For ArrayList, the removeAll method takes time proportional to the size of the list times the lookup time for the argument collection. Using a HashSet as argument should minimize the time it takes.
If you only remove a few values, any collection will probably suffice.
LinkedList tmpLinkedList = new LinkedList(result);
for (Iterator iter = tmpLinkedList.iterator(); iter.hasNext() {
if (iter.next().split("\\s").length != maxLength))
iter.remove();
}
result.clear();
result.addAll(tmpLinkedList);
Or:
HashSet toRemove = new HashSet();
//for (Iterator iter = myarraylist.iterator(); iter.hasNext() {
for (String s : result) {
if (s.split("\\s").length != maxLength)
toRemove.add(elem);
}
result.removeAll(toRemove);