there was some troble because the last index of arraylist. if it was added in the end , it will be add again and again infinite time. i solved this problem. but subtasks not pass. as a result, the task was not completed. only one subtask not pass where if 'r' and 'l' in index not touch bit, my code not touch this index. but all works. maybe i finded alternative solution. this right but the bot cant distinguish my solution from the written one.
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
list = fix(list);
for (String s : list) {
System.out.println(s);
}
}
public static ArrayList<String> fix(ArrayList<String> list) {
int i = 0, c = list.size();
while ( i < c)
{
// System.out.println(" list size " + list.size());
// System.out.println( i + " index , string " + list.get(i));
int a1 = 0, a2 = 0;
// char compare
for (int x = 0; x < list.get(i).length(); x++)
{
char ch = list.get(i).charAt(x);
if ( ch == 'r' ) a1 = 1;
if ( ch == 'l' ) a2 = 1;
} // for x
i++;
if ((a1 == 1) && ( a2 == 0)) { list.remove(i - 1); i--; }
if ((a1 == 0) && ( a2 == 1)) { list.add(list.get(i - 1)); c--;}
// if ((a1 == 1) && ( a2 == 1)) {System.out.println (" dont touch ");}
} // while
return list;
} // method
} // class
/* just comment
if ((a1 == 1) && ( a2 == 0)) {System.out.println (" removed " + list.get(i - 1) ); list.remove(i - 1); i--; }
if ((a1 == 0) && ( a2 == 1)) {System.out.println (" added" + list.get(i - 1) ); list.add(list.get(i - 1)); c--;}
if ((a1 == 1) && ( a2 == 1)) {System.out.println (" dont touch ");}
*/