Java – how to loop through a list of byte arrays…

bytearrayjavalistloops

if i have a list of byte arrays, and i want to delete an array if the first index is a specific value, how would i use something like this:

bytearrays.removeAll(Collections.singletonList(-7));

if this is not possible, what would be the best way to go about this? I've tried using iterators, but i don't think i understand them enough to be able to force it to check the first index of each array in the list of arrays…

when i simply create a for loop, for some reason some of the arrays are not deleted(usually the last array, sometimes the last 2 depending on how many arrays).

Best Solution

The majority of lists have an iterator that correctly implements the remove method.

Given that the simplest solution would be something like the following:

final Iterator<byte[]> iter = bytearrays.iterator();
while (iter.hasNext()) {
    final byte[] temp = iter.next();
    // TODO check for zero-length arrays
    if (temp[0] == -7) {
        iter.remove();
    }
}