I am unable to understand this runtime error since I am only trying to insert 1 element in the list...
java.lang.OutOfMemoryError:
Solution.java, method fix, line: 35
But if I just change the order of input, the error vanishes...
list.add("rose"); // 0
list.add("love"); // 1
list.add("lyre"); // 2
But the output is something like
love
lyre
Insted of...
love
love
lyre
And I am unable to find the problem with
else if ((list.get(i)).contains("l")) {
list.add(i, list.get(i));
}
Help would be appreciated...Thank You!package com.codegym.task.task07.task0716;
import java.util.ArrayList;
import java.lang.String;
/*
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 = fix(list);
for (String s : list) {
System.out.println(s);
}
}
public static ArrayList<String> fix(ArrayList<String> list) {
for (int i=0; i<list.size(); i++) {
if (((list.get(i)).contains("r")) && ((list.get(i)).contains("l"))) {
continue;
}
else if ((list.get(i)).contains("l")) {
list.add(i, list.get(i));
}
else if ((list.get(i)).contains("r")) {
list.remove(i);
}
}
return list;
}
}