Search code examples
javaarraylistcontinuous

Arraylist index calculation java


I have array list that each indexes has value either 0 or 1.I want to find that continuously three indexes has the value 1.How do I find it?


Solution

  • Without actual code:

    1. iterate thorough the array
    2. count the 1s you found
    3. if you found a 0 then start over the count
    4. check after increasing the counter: if the counter reaches 3 then return with true
    5. if you reached the end of the array and no more elements then return with false

    Something like this, I hope I could help.