title: Stuck in while loop / index out of bounds exception
body: <p>I am trying to figure out when a step occurs. So I have written a method called countSteps to do this. The problem with it is that I get stuck in it because with the while loop I keep getting new data and I don't think it ever returns back to onSensor. I also get an error called indexoutofboundsexception: invalid index 2,size 2.</p>
  
  <p>So my first question is are there any other ways to implement the method I have without the while loop? Second is how can I fix the indexoutofboundsexception. 
   public class MainActivity extends Activity implements SensorEventListener {</p>
  
  <pre><code>    private SensorManager mSensorManager;
      private Sensor mRotationVector;
      private Sensor mAccelerometer;
      private TextView mTextView4;
      private TextView mTextView5;
      private TextView mTextView6;
      private TextView mTextView7;
      private TextView mTextView8;
      float a, b, c, d, x, y, z, xyz;
      float[] retVals = new float[3];
      float avg = 10;
      float factor = (float) 1.15;
      ArrayList&lt;Float&gt; accelData = new ArrayList&lt;Float&gt;();
      public int peakCounter = 0;
      public int underAvgCounter = 0;
  
      public void countSteps() {
          int n = 0;
          float controlPoint = accelData.get(0);
          while (accelData.iterator().hasNext()) {
              if (accelData.get(n) != accelData.get(n + 1)) {
                  if (accelData.get(n) &gt; accelData.get(n + 1)) {
                      if (accelData.get(n) &lt; controlPoint) {
                          n++;
                      } else {
                          if (accelData.get(n) &lt; avg * factor) {
                              underAvgCounter++;
                          }
                          peakCounter++;
                          n++;
                      }
                  } else {
                      controlPoint = accelData.get(n + 1);
                      n++;
                  }
              } else {
                  n++;
              }
              peakCounter -= underAvgCounter;
          }
      }
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          mTextView4 = (TextView) findViewById(R.id.textView4);
          mTextView5 = (TextView) findViewById(R.id.textView5);
          mTextView6 = (TextView) findViewById(R.id.textView6);
          mTextView7 = (TextView) findViewById(R.id.textView7);
          mTextView8 = (TextView) findViewById(R.id.textView8);
          mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
          mAccelerometer = mSensorManager
                  .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
          accelData.add((float) 0);
      }
  
      protected void onResume() {
          super.onResume();
          mSensorManager.registerListener(this, mAccelerometer,
                  SensorManager.SENSOR_DELAY_GAME);
      }
  
      protected void onPause() {
          super.onPause();
          mSensorManager.unregisterListener(this);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      public void onAccuracyChanged(Sensor sensor, int accuracy) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void onSensorChanged(SensorEvent event) {
              x = event.values[0];
              y = event.values[1];
              z = event.values[2];
              xyz = (float) Math.sqrt((x * x) + (y * y) + (z * z));
              accelData.add(xyz);
              mTextView7.setText("magnitude accel " + xyz);
              countSteps();
              mTextView8.setText("steps " + peakCounter);
  
      }
  }
  </code></pre>