简体   繁体   中英

How to stop for loop once it has been looped to all records in a list

I am using for loop in my program, and i want to know how can i stop this once it has been looped to all records in a list one by one.

btnSyncAll.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            for (int i=0; i<ImageList.size(); i++)
            {
                new SyncFileAsync().execute(String.valueOf(i));                                                            
            }   
        }
    });

The idea of a for loop is that it stops after it looped from int i = 0 to i < imageList.size() . I dont know why you have the feeling that it doesnt, but probably your imageList is really big and it takes a lot of time until the loop is finished.

For loop stops iterating when it reaches the limit you defined in for loop statement. But if you want to stop in middle you have to put break with certain condition check.

ie

for(int i=0;i < n;i++){

   if(i==4){
          break;
   }

}

if you want to not to execute for particular iteration use continue statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM