add values to an array

stanstan canada ontario

Hi there am new to java.
have this issue on my code that bugging me. will be very grateful for any help:
here's the problem:
am trying to write a code that computes the length of maximal occurance of true in a boolean array. then after this I create an integer array which is going to have its 1st value as the length of conservative occurrence of trues in the iput array and the next elements are the indexes of the starting points (in the input array values) of each of the maximal sequences of consecutive trues:
here is what I have done sofar:

public static int[] longestSequence(boolean[] values)
{
int count = 0;
int[] result = new int[count+1];

    for (int i=0; i<values.length; i++)
    {
        /*(if the value of the 1st index is true AND the index is zero)
        or (if the value of the 1st index is true and value of one index less than the current is not true
           then just set count to 1) */
        if(values[i] && (i==0 || !(values[i-1])))
        {
            count = 1;
        }
        /*if index of i is strictly greater than zero and both value of ith and (i-1)th 
        index are true, then increment i by 1.*/
        if(i>0 && values[i-1] && values[i])
        {
            count +=1;
            int j=1;
            while(j<=count)
            {
            result[j]= i-1;
            result[j+1] = i;
            }

        }

    }   
    result[0]= count;
    return result;

}
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories