Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count items in Arraylist that have specific attribute

I want to execute code if NONE of the items in Arraylist have attribute 'slotted' set to false. I use the following code :

int p=0;

 for (int i = 0; i < AppleList.size();i++){
     if (AppleList.get(i).slotted = true){
        p++;
     } 

if (p == 0){

    //EXECUTE CODE

}

Is there any way to do this better?

like image 917
Kees Koenen Avatar asked Jan 20 '26 13:01

Kees Koenen


1 Answers

You can do it using enhanced-for loop and a label:

label:
{
    for(Foo f : AppleList) if(!f.slotted) break label;

    // Here you guaranteed that all slotted fields are true
}
like image 66
Eng.Fouad Avatar answered Jan 23 '26 03:01

Eng.Fouad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!