Following code works great until the last element in the ArrayList "LOL", which should be duplicated as per the rules but instead it get stuck in an infinate loop and the output (including the debug output) is Current list: [love, love, lyre, LOL] Debug three LOL 3 Current list: [love, love, lyre, LOL] LOL 3 Current list: [love, love, lyre, LOL] LOL 3 Forever.
package com.codegym.task.task07.task0716;

import java.util.ArrayList;

/*
1. Create a list of words and populate it yourself.
2. The fix method should:
2.1. remove all words containing the letter "r" from the list
2.2. duplicate all words containing the letter "l".
2.3. if a word contains both "r" and "l", then leave it unchanged.
2.4. don't do anything to other words.

*/

public class Solution {
    public static void main(String[] args) throws Exception {
        ArrayList<String> list = new ArrayList<String>();
        list.add("rose"); // 0
        list.add("love"); // 1
        list.add("lyre"); // 2
        list.add("measure");
        list.add("LOL");
        list = fix(list);

        for (String s : list) {
            System.out.println(s);
        }
    }

    public static ArrayList<String> fix(ArrayList<String> list) {
        // write your code here

        for(int i = 0; i < list.size();){
            //Debug output
            System.out.println(list.get(i));
            System.out.println(i);
            System.out.println("Current list: " +  list);
            //End debug output

            if(list.get(i).contains("r") && list.get(i).contains("l")){
               System.out.println("Debug three");
                i++;
            }

            if(list.get(i).contains("l") && !list.get(i).contains("r")){
                list.add(i,list.get(i));
                i = i + 2;
              System.out.println("Debug one");
            }
            if(list.get(i).contains("r") && !list.get(i).contains("l")){
                list.remove(i);
              System.out.println("Debug two");
                if(i > 0){
                    i--;
                }
                else{
                    i = i;
                }
            }
        }
        return list;
    }
}