Why is the below code throwing internal error while run code is successful


  • 0
    L
    public class MovingAverage {
    
    ArrayList<Integer> arrayList;
    int size;
    
    /** Initialize your data structure here. */
    public MovingAverage(int size) {
        this.size = size;
        this.arrayList = new ArrayList<>(size);
    }
    
    public double next(int val) {
        
        int sum = 0;
    
        try {
            if ((!arrayList.isEmpty()) && arrayList.size() == this.size) {
                arrayList.remove(0);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    
        arrayList.add(new Integer(val));
    
        for(Integer integer : arrayList) {
            sum += integer.intValue();
        }
    
        return (double)sum/arrayList.size();
    }
    }

  • 0

    This had just been fixed, please try again.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.