Using Queue.

```
private Queue<Integer> que = new LinkedList<>();
private int capacity = 0;
private int sum = 0;
/** Initialize your data structure here. */
public MovingAverage(int size) {
capacity = size;
}
public double next(int val) {
sum += val;
que.offer(val);
if(que.size() > capacity){
sum -= que.poll();
}
return sum / que.size();
}
```

Using Array.

```
private int[] arr;
private int sum = 0;
private int index = 0;
/** Initialize your data structure here. */
public MovingAverage(int size) {
arr = new int[size];
}
public double next(int val) {
sum += val;
index = index % arr.length;
sum -= arr[index];
arr[index] = val;
++index;
return sum / (Math.min(index, arr.length));
}
```