ArrayList<SensorEvent>.add(...) begins overwriting values
<p>I am writing an app that needs to keep the values that come from the accelerometer. I am doing this by using an ArrayList and then when onAccelerationChanged(...) gets called I simply add those values to the list. The problem is that after so many values (never consistent) get pushed into the list it starts overwriting the values with the value I am adding. I figured I was doing something stupid so I took the 'Accelerometer Play Sample' and modified onSensorChanged(...) to do the same. Doing this replicates the issue.</p>

<p>variable added to class:</p>

<pre><code>private ArrayList&lt;SensorEvent&gt; _accelData = new ArrayList&lt;SensorEvent&gt;();
</code></pre>

<p>onSensorChanged:</p>

<pre><code>@Override
public void onSensorChanged(SensorEvent event)
{
  if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
    return;

  Log.e("Event to Add", event.timestamp + ", " + event.accuracy + ", " + event.values[0] + ", " + event.values[1]
      + ", " + event.values[2]);

  _accelData.add(event);

  if (_accelData.size() &gt; 25) {
    for (SensorEvent tmp_event : _accelData) {
      Log.e("Events in list", tmp_event.timestamp + ", " + tmp_event.accuracy + ", " + tmp_event.values[0] + ", "
          + tmp_event.values[1] + ", " + tmp_event.values[2]);
    }

    _accelData.clear();
  }
}
</code></pre>

<p>On the left is each event that was supposed to be added and on the right is the list printed out after 26 adds were performed. As you can tell the values that were supposed to be added don't match the list itself and there is a ton of repetition of the last and second to last value in the list. </p>

<p><img src="http://i.stack.imgur.com/I7kNu.png" alt="enter image description here"></p>

<p>I fixed this by making a new class that takes in an event and copies those values. Then I make the ArrayList take that class instead. It works but I don't understand why and was hoping someone could educate me since I am new to Java and Android.</p>
