0

The loop below seems to stop short, and then restart. It is not inside of another loop. The first Log call prints 36, thus the outer for loop should run 36 times. The Log call inside of the loop though, which is meant to print the number of times the loop has run, prints "0" up to "4" meaning the loop only ran 5 times. Would there be any reason for this process to start over so that the first Log call fires again, and the loop again runs through only 5 times? This occurs twice according to my Logcat output.

ArrayList<RunData_L> rdUP = t.getMyPositiveRunData();
ArrayList<RunData_L> rdDOWN = t.getMyNegativeRunData();
Log.d("rdUP size", rdUP.get(0).getMyMeasurementData().size() + "");
for (int i = 0; i < rdUP.get(i).getMyMeasurementData().size(); i++) {
   Log.d("i", i + "");
   ArrayList<BigDecimal> tempUP = new ArrayList<BigDecimal>(), tempDOWN = new ArrayList<BigDecimal>();
   for(int j = 0; j < rdUP.size(); j++) {
      tempUP.add(rdUP.get(j).getMyMeasurementData().get(i));
      tempDOWN.add(rdDOWN.get(j).getMyMeasurementData().get(i));
   }
   pdUP.add(tempUP);
   pdDOWN.add(tempDOWN);
}
Jonathan Wrona
  • 423
  • 2
  • 7
  • 19
  • your outer loop is using `rdUP.get(i)` but your inner loop is using `rdUP.get(j)` to me this is wrong. I'm not sure you've coded what you think you've coded. Can you explain specifically what you want to happen? – Jon Taylor Jul 26 '12 at 18:54
  • Keep in mind that when working with nested loops, the outer loop changes only after the inner loop is completely finished – jnthnjns Jul 26 '12 at 19:00
  • @JonTaylor I'm not sure what was wrong with the loop restarting but I changed the variable that i was counting up to in the first loop to one which should be constant and that worked! Thank you, I'm not sure what was wrong haha. – Jonathan Wrona Jul 26 '12 at 19:01
  • As per my answer you cant rely on the knowledge that the size of `rdUP.get(i).getMyMeasurementData()` will be the same as `rdUP.get(!1).getMyMeasurementData()` which is what your code is doing. – Jon Taylor Jul 26 '12 at 19:03

1 Answers1

0

I suspect as per my comment that the use of rdUP.get(j) in the inner loop is causing the problem.

You first test to see the size of rdUP.get(i).getMyMeasurementData() in the outer loop as your bounding condition for the loop and so i runs from 0 to the size of rdUP.get(i).getMyMeasurementData().

Your inner loop then says go through each element of rdUP and get the i th value from rdUP.get(j).getMyMeasurementData(). How do you know that the j th rdUP has enough elements to satisfy your get(i) ?

Jon Taylor
  • 7,865
  • 5
  • 30
  • 55