0

I am trying to use an iterator on an ArrayList ( to get rid of a for loop, don't ask me why... ), however I need to skip the process of one of the arrays upon a boolean condition , should I still use an index and a break ???

// INTERPOLATION
int i = 0;
Iterator<CircularFifoQueue<SensorEvent>> buf = samplingFifoQueues.iterator();
while (buf.hasNext()) {
    if ( i == 2 && !mDeviceSensorGyro) {  // skip this queue if no gyroscope in device
       break;
    }               
    // proceed
   buf.next();
   i++;
}

thanks for help

7
  • 1
    Do you want to continue over to the next element's processing or just want to break completely out of it? Commented Feb 7, 2014 at 10:39
  • @PopoFibo skip means skip, doesn't it? Commented Feb 7, 2014 at 10:59
  • @Gangnus well it does yes but the statement wasn't perfectly clear to me hence the comment, doesn't that align to the correct usage of the comments section after all? Commented Feb 7, 2014 at 11:25
  • @PopoFibo In comments it is skip, too. in CODE there is a break, yes. The task is set by words, code IS incorrect, according to the questionner. Commented Feb 7, 2014 at 11:27
  • @Gangnus sir yes sir :) Commented Feb 7, 2014 at 11:37

1 Answer 1

1

What about this:

// INTERPOLATION
int i = 0;
Iterator<CircularFifoQueue<SensorEvent>> buf = samplingFifoQueues.iterator();
while (buf.hasNext()) {
    if ( i != 2 || mDeviceSensorGyro) {  // skip this queue if no gyroscope in device
       // proceed  
    }               

    buf.next();
    i++;
}

But I would rather attach some attribute to the queue elements to check for it. Work directly with numbers is bad practice.

Sign up to request clarification or add additional context in comments.

2 Comments

this will work fine... however I inserted a test buf.isEmpty() to skip it, if it doesn't exist there are no data captured...
@erwin Of course. It is a normal practice to have many conditions under which the different branches are taken inside and out of the cycle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.