Results 1 to 5 of 5
- 02-21-2011, 02:01 AM #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[1] = 0, then x would go into array[1]. So, if array[1] != 0, then i want to put "x" into the next xth element, if that next element is 0. So if x was 3, array[1]!=0 so then it checks array[3]. array[3] =0, so x goes into array[3]. heres my question. it checks array[3], and it !=0, so i go to the next xth element, which is array[6] in this case. array[6]!=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[0]. so the next comparison it would make would be at array[2]. 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.
- 02-21-2011, 02:08 AM #2
Use a while loop instead.
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 }
- 02-21-2011, 02:59 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
worked like a charm. thanks a bunch
- 02-21-2011, 03:45 AM #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 } }
- 02-21-2011, 03:52 AM #5
Similar Threads
-
How to start a for loop back to top without starting the var count over?
By AcousticBruce in forum New To JavaReplies: 3Last Post: 01-07-2011, 12:23 AM -
[Java3D] Shape3D to byte array and back
By Dennis in forum Advanced JavaReplies: 0Last Post: 12-07-2010, 03:32 PM -
creating a jump
By pizzadude223 in forum Java 2DReplies: 6Last Post: 07-21-2010, 05:01 PM -
Eclipse- jump to method definition
By Java Tip in forum Java TipReplies: 0Last Post: 11-07-2007, 03:34 PM -
To truncate jump of line in jsp
By Eric in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 06-09-2007, 03:58 AM
Bookmarks