java foreach skip first iteration

前端 未结 11 933
刺人心
刺人心 2021-02-01 12:05

Is there an elegant way to skip the first iteration in a Java5 foreach loop ?

Example pseudo-code:

for ( Car car : cars ) {     
   //skip if first, do w         


        
11条回答
  •  隐瞒了意图╮
    2021-02-01 12:48

    You can use a counter. Though not so mature coding, still I find it the easiest way to skip the first element from a list.

        int ctr=0;
        for(Resource child:children) {
        if(ctr>0) { //this will skip the first element, i.e. when ctr=0
        //do your thing from the 2nd element onwards
        }
        ctr++;
        }
    

提交回复
热议问题