# Thread: how to loop through an array and jump back to beginning

1. Member Join Date
Feb 2011
Posts
4
Rep Power
0

## how to loop through an array and jump back to beginning

I'm trying to write a program and part of it is stumping me. I have an array of seven integers. The default value of each is 0. Some have other values though. Here's my issue. When the element is 0, i want to replace the value with x. For example, if array = 0, then x would go into array. So, if array != 0, then i want to put "x" into the next xth element, if that next element is 0. So if x was 3, array!=0 so then it checks array. array =0, so x goes into array. heres my question. it checks array, and it !=0, so i go to the next xth element, which is array in this case. array!=0. now i want to go to the next xth element, but there are no elements, so i want to loop back around to the beginning, starting at array. so the next comparison it would make would be at array. how do i write a loop like this? in order to find the zero, it checks every xth element through the array, looping around until it finds a zero. there is guaranteed to be a zero somewhere.
thanks a bunch.  Reply With Quote

2. ## Java Code:
```while not finished {
if array element is 0 {
change array element
update x
if x is greater than length {
reset back to zero (or what ever)
}
}
determine if finished
}```  Reply With Quote

3. Member Join Date
Feb 2011
Posts
4
Rep Power
0

## worked like a charm. thanks a bunch  Reply With Quote

4. Member Join Date
Feb 2011
Posts
10
Rep Power
0

## you can try this code

Java Code:
```      for(int i=0;i<array.length;i++){
if(array[i]==0){
//....update code
}
}```  Reply With Quote

5. ## You could but it wouldn't do what the OP wants.  Reply With Quote

array, element, loop 