Can anyone please let me know what is wrong with my code? I don't necessarily want a solution, just wanna understand why do i get the "java heap space " error and how to fix it. Thank you in advance
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("love"); // 0
list.add("lyre"); // 1
list.add("love"); // 2
list.add("measure");
list.add("mustacher");
list = fix(list);
for (String s : list) {
System.out.println(s);
}
}
public static ArrayList<String> fix(ArrayList<String> list) {
// write your code here
ArrayList<String> list1 = new ArrayList<>();
for(String x : list ) {
if (!x.contains("r") && x.contains("l")) {
list1.add(x);
} else if (x.contains("r") && x.contains("l")) {
list1.add(x);
}
}
for(int i = 0; i < list1.size(); i++){
if(!list1.get(i).contains("r")){
list1.add(list1.get(i));
}
}
return list1;
}
}