I'm trying to Write a method called scaleByK that takes an ArrayList of integers as a parameter and replaces every integer of value k with k copies of itself. For example, if the list stores the values [4, 1, 2, 0, 3] before the method is called, it should store the values [4, 4, 4, 4, 1, 2, 2, 3, 3, 3] after the method finishes executing. Zeroes and negative numbers should be removed from the list by this method. the problem is that I'm getting error of java heap space.
public class Lab1construct2 {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(4);
        list.add(1);
        list.add(2);
        list.add(0);
        list.add(3);
        Lab1construct2 myprogram = new Lab1construct2();
        myprogram.scalebyk(list);
        System.out.print(list);
    }
    public void scalebyk(ArrayList<Integer> list) {
        int j = 0;
        while( j < list.size()) {
            int elm =list.get(j);
            for(int i=1; i<=elm; i++) {
                list.add(elm);
            }
            j = j + list.get(j);
        }
    }
}
