Java finding start and end of each sub-sequence in an int array -


i have array containing sequence of numbers below, sequence of number of pixels in y axis (horizontal projection histogram):

[ 0 0 3 13 16 16 18 19 19 18 14 10 8 0 0 0 0 0 7 13 15 16 19 20 18 17 14 9 0 0 0 0 ]       ^                            ^     start                         end 

how can find starting index , ending index of each sub-sequence in array?
expect in example this: first sub-sequence: startindex = 2, endindex = 12 , second sub-sequence: startindex = 18, endindex = 27.

what have came with:

for(int =0; i<pixels.length; i++){         system.out.println(pixels[i]);         if(pixels[i] != 0) {             start = i;             system.out.println("start= " + start );         } else if(pixels[i] == 0){             end = i;             system.out.println("end= " + end);         }     } 

i appreciate help.

when iterating trough array, don't keep track if started sequence.

with small change should work

int start=-1; for(int =0; i<pixels.length; i++){         system.out.println(pixels[i]);         if(pixels[i] != 0 && start == -1) {             start = i;             system.out.println("start= " + start );         } else if(pixels[i] == 0 && start != -1;){             end = i;             start = -1;             system.out.println("end= " + end);         }     } 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -