Please help me trying to get this code working! When I run it, it seems it falls into an endless loop, printing out "test" unlimited times and also the size of the list increases unbelievably fast.
I think it has to do with the removal of the list item before. The removal is working perfectly fine, also when I ommit the line with the list.add()!
public class Solution {
public static void main(String[] args) throws Exception {
ArrayList<String> liste = new ArrayList<String>();
liste.add(0,"Rose"); // 0
liste.add(1,"Leier"); // 1
liste.add(2,"Liebe"); // 2
liste = korrigieren(liste);
for (String s : liste) {
System.out.println(s);
}
}
public static ArrayList<String> korrigieren(ArrayList<String> liste) {
String check;
for (int j = 0; j < liste.size(); j++) {
check = liste.get(j);
if ((check.contains("r") || check.contains("R")) && !(check.contains("l") || check.contains("L"))) { //R but NOT L
liste.remove(j);
} else if ((check.contains("l") || check.contains("L")) && !(check.contains("r") || check.contains("R"))) { //L but NOT R //
liste.add(j, liste.get(j));
System.out.println("test");
System.out.println(liste.size());
}
}
return liste;
}
}