Can you reset the counter for each cycle?

since there are no available counters or i there to reset to zero.

Is there a way around with continue or break to achieve this?

 class CommandLine { public int[] sort(int array[]) { int temp; for (int i=0; i<array.length-1; i++) {//must have used for-each if (array[i] > array[i+1]){ temp = array[i]; array[i] = array[i+1]; array[i+1] = temp; i=-1; }//if end }//for end return array; }//main end }//class end 
+5
source share
2 answers

There is usually no way to do this: the iterator that is used to β€œdrive” the for-each loop is hidden, so your code does not have access to it.

Of course, you can create a class that looks like a collection, which maintains references to all the iterators it creates, and allows you to reset from the side. However, this qualifies as hacking and should be avoided.

If you need a way to reset your iteration to a specific location, use the usual for loop.

+2
source

No, no counter is used for each cycle inside. So just use a regular loop, for example:

 for (int i = 0; i < myCollection.size(); i ++) { // do something } 

More info: Is there a way to access the iteration counter in a Java loop for each loop?

+1
source

Source: https://habr.com/ru/post/1215945/


All Articles