I have tried printing out whole array in "print 1" and "print 2" first one prints out the list and the second one does not print nothing just "null" so somehow my "list = fix(list)" makes the arraylist null? any ideas why?
package com.codegym.task.task07.task0716;
import java.util.ArrayList;
/*
R or L
*/
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
// print 1
list = fix(list);
// print 2
for (String s : list) {
System.out.println(s);
}
}
public static ArrayList<String> fix(ArrayList<String> list) {
for(int i = 0; i < list.size(); ) {
String s1 = list.get(i);
if(s1.contains("r") && s1.contains("l")) {
i++;
} else if(s1.contains("r")) {
list.remove(i);
} else if(s1.contains("l")) {
list.add(i, list.get(i));
i++;
i++;
} else {
i++;
}
}
return null;
}
}