Results 1 to 5 of 5
  1. #1
    drhinri is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Question 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.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    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
    }

  3. #3
    drhinri is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    worked like a charm. thanks a bunch

  4. #4
    liyuyu_852000 is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default you can try this code

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

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

Similar Threads

  1. Replies: 3
    Last Post: 01-07-2011, 12:23 AM
  2. [Java3D] Shape3D to byte array and back
    By Dennis in forum Advanced Java
    Replies: 0
    Last Post: 12-07-2010, 03:32 PM
  3. creating a jump
    By pizzadude223 in forum Java 2D
    Replies: 6
    Last Post: 07-21-2010, 05:01 PM
  4. Eclipse- jump to method definition
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-07-2007, 03:34 PM
  5. To truncate jump of line in jsp
    By Eric in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-09-2007, 03:58 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •