# Looping through an array?

**URL:** <https://discourse.processing.org/t/looping-through-an-array/13078>\
**Category:** Beginners\
**Created:** [July 30, 2019, 11:47pm UTC](https://discourse.processing.org/t/looping-through-an-array/13078 "2019-07-30T23:47:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pbchocochipcheerios](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@Pbchocochipcheerios](https://discourse.processing.org/u/Pbchocochipcheerios)\
**Post date:** [July 30, 2019, 11:47pm UTC](https://discourse.processing.org/t/looping-through-an-array/13078/1 "2019-07-30T23:47:20Z")

</div>

If I have an array with 14 elements, Is there a way to loop through the array so that when I get to array[13], it automatically goes back to array[0] up to array [13] and loop back to array[0] etc etc?

I was thinking modulo, but I am not sure where I would place that. Then I was thinking maybe I can make the array 15 elements, and when it reaches [15], set that array[15] = array[0] and somehow get it to loop again.

This array will be in a ‘for loop’ where I will be using array[i] and saying array[i] ++; I want it to continuously add +1 until I reach the “max”, or whatever i \< max.

I am very new to processing, any help would be greatly appreciated! I don’t want to paste my code as it is not written well and is quite lengthy!

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [July 31, 2019, 12:25am UTC](https://discourse.processing.org/t/looping-through-an-array/13078/2 "2019-07-31T00:25:20Z")

</div>

> [@Pbchocochipcheerios](#):
>
> I don’t want to paste my code as it is not written well and is quite lengthy!

right, that might even stop readers from helping,  
but what you should do is to make a extra small program  
( complete / run-able for the problem only )  
where we can discuss your question with.

and also give it a nice format, like

```auto
// https://discourse.processing.org/t/looping-through-an-array/13078
// _________________________________________________ TEST array def
int[] numbers = { 90, 150, 30, 7, 8, 0, 22, 44, 36, 52, 82, 31, 63, 39 };
// _________________________________________________ SETUP
void setup() {
  printArray(numbers);
}
// _________________________________________________ DRAW
void draw() {
  for ( int i = 0; i < 14; i++ ) println("_"+i+"_: "+numbers[i]);
  println("next draw loop");
}

```

---

<div class="post-metadata">

**Author:** ![Pbchocochipcheerios](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@Pbchocochipcheerios](https://discourse.processing.org/u/Pbchocochipcheerios)\
**Post date:** [July 31, 2019, 1:02am UTC](https://discourse.processing.org/t/looping-through-an-array/13078/3 "2019-07-31T01:02:39Z")

</div>

Okay, sorry, I put together a really simple code. So in my code, int startingPoint will change depending on some other things so I want to be sure that when startingPoint is greater than 9, that it will loop the array back to [0] and add 1 to that. Right now, my program just crashes when I have a starting number of 10 or greater.

Here is the code:

```auto
int[] myArray = new int [14];
int startingAmount = 4; //initial value in each index
int startingPoint = 10; //starting index

void initialNumbers()
{
  for (int i = 0; i < myArray.length; i ++)
  {
    myArray[i] = startingAmount; //sets all to starting amount
  }
}

void addNumber()
{
  for (int i = 0; i < startingAmount+1; i ++)
  { 
    myArray[startingPoint+i] ++;
  }
  myArray[startingPoint] = 0;
}

void setup()
{
  initialNumbers();
  addNumber(); 
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [July 31, 2019, 1:15am UTC](https://discourse.processing.org/t/looping-through-an-array/13078/4 "2019-07-31T01:15:01Z")

</div>

yes, array what is long 14  
has a possible index from 0 … 13  
so a

```auto
  for (int i = 0; i < startingAmount; i++) {

```

works, still the result is questionable  
WHY YOU NOT PRINT ANYTHING

```auto
int[] myArray = new int [14];
int startingAmount = 4; //initial value in each index
int startingPoint = 10; //starting index

void initialNumbers() {
  for (int i = 0; i < myArray.length; i ++) myArray[i] = startingAmount; //sets all to starting amount
}

void addNumber() {
// for (int i = 0; i < startingAmount+1; i++) {
  for (int i = 0; i < startingAmount; i++) {
    println("add "+i);
    myArray[startingPoint+i]++;
  }
  myArray[startingPoint] = 0;
}

void setup() {
  initialNumbers();
  addNumber();
  println(myArray);
}

```

shows in console

```auto
add 0
add 1
add 2
add 3
[0] 4
[1] 4
[2] 4
[3] 4
[4] 4
[5] 4
[6] 4
[7] 4
[8] 4
[9] 4
[10] 0
[11] 5
[12] 5
[13] 5

```

---

<div class="post-metadata">

**Author:** ![Pbchocochipcheerios](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@Pbchocochipcheerios](https://discourse.processing.org/u/Pbchocochipcheerios)\
**Post date:** [July 31, 2019, 1:22am UTC](https://discourse.processing.org/t/looping-through-an-array/13078/5 "2019-07-31T01:22:52Z")

</div>

So with startingPoint = 10, I want:

[10] 0  
[11] 5  
[12] 5  
[13] 5  
[0] 5

with startingPoint = 11, I want:

[10] 4  
[11] 0  
[12] 5  
[13] 5  
[0] 5  
[1] 5

I am sorry I didn’t put print in there, I am so new that even printing things confuses me 😩

I appreciate your help though, I really do!!!

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [July 31, 2019, 5:17am UTC](https://discourse.processing.org/t/looping-through-an-array/13078/6 "2019-07-31T05:17:40Z")

</div>

so you must roll around your pointer

```auto
int along = 14;
int[] myArray = new int [along];
int startingAmount = 4; //initial value in each index
int startingPoint = 10; //starting index
boolean diagp = true;

void initialNumbers() {
  for (int i = 0; i < myArray.length; i ++) myArray[i] = startingAmount; //sets all to starting amount
}

void addNumber() {
  for (int i = 0; i < startingAmount+1; i++) {
    int j = startingPoint+i;
    if ( j >= myArray.length ) j -= myArray.length;
    myArray[j]++;
    if ( diagp ) println("add i: "+i+ " j: "+j+ " myArray["+j+"]="+myArray[j]);
  }
  myArray[startingPoint] = 0;
}

void setup() {
  initialNumbers();
  addNumber();
  if ( diagp ) for ( int i = 0; i < myArray.length; i++ ) print(myArray[i]+" , ");
}

```

shows

```auto
add i: 0 j: 10 myArray[10]=5
add i: 1 j: 11 myArray[11]=5
add i: 2 j: 12 myArray[12]=5
add i: 3 j: 13 myArray[13]=5
add i: 4 j: 0 myArray[0]=5
5 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , 0 , 5 , 5 , 5 , 

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 31, 2019, 11:50am UTC](https://discourse.processing.org/t/looping-through-an-array/13078/7 "2019-07-31T11:50:21Z")

</div>

Exactly, you don’t change the array but you change the index (the line number of the array) to make the index do what you want.

---

<div class="post-metadata">

**Author:** ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)\
**Post date:** [July 31, 2019, 12:09pm UTC](https://discourse.processing.org/t/looping-through-an-array/13078/8 "2019-07-31T12:09:23Z")

</div>

Hey There!

Modulo is the way to do it !

```auto
for int i = 0 true i++
      ar[i%ar.length]

```

---

<div class="post-metadata">

**Author:** ![Pbchocochipcheerios](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@Pbchocochipcheerios](https://discourse.processing.org/u/Pbchocochipcheerios)\
**Post date:** [July 31, 2019, 3:22pm UTC](https://discourse.processing.org/t/looping-through-an-array/13078/9 "2019-07-31T15:22:52Z")

</div>

Thank you so much! I think I understand now. It works great when I call that function in setup, but is there a way to get this function to work in draw so I can change the startingAmount value based on other functions? When I move the function into draw, no matter what my startingAmount is, I get an error saying out of bounds

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 31, 2019, 3:28pm UTC](https://discourse.processing.org/t/looping-through-an-array/13078/10 "2019-07-31T15:28:38Z")

</div>

Of course you can

Show your entire code

Otherwise we can’t know what you did wrong

---

<div class="post-metadata">

**Author:** ![Pbchocochipcheerios](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@Pbchocochipcheerios](https://discourse.processing.org/u/Pbchocochipcheerios)\
**Post date:** [July 31, 2019, 3:36pm UTC](https://discourse.processing.org/t/looping-through-an-array/13078/11 "2019-07-31T15:36:48Z")

</div>

I am playing around with @kll’s above code, except I moved “addNumber();” to void draw();. I guess because draw is continuously running, it keeps adding and adding, but I thought the for loop conditions would stop this!

> [@kll](#):
>
> so you must roll around your pointer
> 
> ```auto
> int along = 14;
> int[] myArray = new int [along];
> int startingAmount = 4; //initial value in each index
> int startingPoint = 10; //starting index
> boolean diagp = true;
> 
> void initialNumbers() {
> for (int i = 0; i < myArray.length; i ++) myArray[i] = startingAmount; //sets all to starting amount
> }
> 
> void addNumber() {
> for (int i = 0; i < startingAmount+1; i++) {
> int j = startingPoint+i;
> if ( j >= myArray.length ) j -= myArray.length;
> myArray[j]++;
> if ( diagp ) println("add i: "+i+ " j: "+j+ " myArray["+j+"]="+myArray[j]);
> }
> myArray[startingPoint] = 0;
> }
> 
> void setup() {
> initialNumbers();
> }
> 
> void draw()
> {
> addNumber();
> if ( diagp ) for ( int i = 0; i < myArray.length; i++ ) print(myArray[i]+" , ");
> }
> 
> ```

Blockquote

---

<div class="post-metadata">

**Author:** ![Pbchocochipcheerios](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@Pbchocochipcheerios](https://discourse.processing.org/u/Pbchocochipcheerios)\
**Post date:** [July 31, 2019, 4:01pm UTC](https://discourse.processing.org/t/looping-through-an-array/13078/12 "2019-07-31T16:01:08Z")

</div>

I got it you guys!!! Thanks so much for everyones help!!! I appreciate it x 1 million

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 31, 2019, 4:01pm UTC](https://discourse.processing.org/t/looping-through-an-array/13078/13 "2019-07-31T16:01:44Z")

</div>

Can you post your final code example please

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [July 31, 2019, 4:03pm UTC](https://discourse.processing.org/t/looping-through-an-array/13078/14 "2019-07-31T16:03:58Z")

</div>

It looks like you are trying to create [Mancala - The African Stone Game](https://www.youtube.com/watch?v=OICVv6Wt7iw) is that correct?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 31, 2019, 4:36pm UTC](https://discourse.processing.org/t/looping-through-an-array/13078/15 "2019-07-31T16:36:20Z")

</div>

```auto

int along = 14;
int[] myArray = new int [along];

int startingAmount = 4; //initial value in each index
int startingPoint = 10; //starting index

boolean diagp = true;

// -----------------------------------------------------------------------------------

void setup() {
  size(300, 300); 
  initialNumbers();
}

void draw() {
  addNumber();
  if ( diagp ) { 
    for ( int i = 0; i < myArray.length; i++ ) 
      print(myArray[i]+" , ");
  }//if
}

// -----------------------------------------------------------------------------------

void initialNumbers() {
  //sets all to starting amount
  for (int i = 0; i < myArray.length; i ++) 
    myArray[i] = startingAmount;
}

void addNumber() {
  //
  for (int i = 0; i < startingAmount+1; i++) {

    int j = startingPoint+i;
    if ( j >= myArray.length ) 
      j -= myArray.length;

    myArray[j]++;

    if ( diagp ) 
      println("add i: "
        +i
        + " j: "
        +j
        + " myArray["+j+"]="
        +myArray[j]);
  }//for
  myArray[startingPoint] = 0;
}
//

```

---

<div class="post-metadata">

**Author:** ![Pbchocochipcheerios](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@Pbchocochipcheerios](https://discourse.processing.org/u/Pbchocochipcheerios)\
**Post date:** [July 31, 2019, 5:01pm UTC](https://discourse.processing.org/t/looping-through-an-array/13078/16 "2019-07-31T17:01:40Z")

</div>

Yup, you are correct!

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [July 31, 2019, 5:17pm UTC](https://discourse.processing.org/t/looping-through-an-array/13078/17 "2019-07-31T17:17:29Z")

</div>

I have never played the game but it suddenly dawned on me that the algorithm seemed familiar.

A quick google showed that although you start with the same game configuration there are several ways or sets of rules to play the game. One of them prevents you placing stones in your opponents home cup but the current algorithm does not allow for that.

---

<div class="post-metadata">

**Author:** ![wrcstewart](https://avatars.discourse-cdn.com/v4/letter/w/ac91a4/32.png) [@wrcstewart](https://discourse.processing.org/u/wrcstewart)\
**Post date:** [August 6, 2019, 8:59am UTC](https://discourse.processing.org/t/looping-through-an-array/13078/18 "2019-08-06T08:59:30Z")

</div>

Hi  
Could you just enclose your for loop in an outer for loop or other looping construct? Initialise your inner loop variable to a start value computed inthe outer loop.

---

<div class="post-metadata">

**Author:** ![Quertz](https://avatars.discourse-cdn.com/v4/letter/q/cc9497/32.png) [@Quertz](https://discourse.processing.org/u/Quertz)\
**Post date:** [August 6, 2019, 3:56pm UTC](https://discourse.processing.org/t/looping-through-an-array/13078/19 "2019-08-06T15:56:57Z")

</div>

I’d write a very small class that does the looping interation. It only offers a constructor, taking the length of the loop, a method “iterate” and a method int value() to get the current index it is pointing on.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [August 6, 2019, 7:52pm UTC](https://discourse.processing.org/t/looping-through-an-array/13078/20 "2019-08-06T19:52:58Z")

</div>

> [@Quertz](#):
>
> a very small class that does the looping interation

Normally I would say there isn’t much more to it than using modulo – so no need, your “class” is int idx, its “next” method is idx=(idx+1)%arr.length, and that’s it.

However, Java’s % is actually the remainder, which causes a gotcha on negatives, so IF you ever wanted to step negative numbers then it might be useful to wrap a loop counter in a method or class.

```auto
/**
 * LoopCounter
 * https://discourse.processing.org/t/looping-through-an-array/13078/19
 **/
void setup() {
  LoopCount lc = new LoopCount(4);
  println(lc.val);
  println(lc.inc(1), lc.inc(1), lc.inc(1), lc.inc(1));
  println(lc.inc(2), lc.inc(2));
  println(lc.inc(7));
  println(lc.inc());
  println(lc.dec(), lc.dec(), lc.dec(), lc.dec());
  println(lc.dec(7));
}

class LoopCount {
  int max;
  int val;
  LoopCount(int max) {
    this.max = max;
  }
  LoopCount(int max, int val) {
    this.max = max;
    this.val = val;
  }
  int dec() {
    return inc(-1);
  }
  int dec(int count) {
    return inc(-count);
  }
  int inc() {
    return inc(1); 
  }
  int inc(int count) {
    this.val = ((val+count)%max + max)%max;
    return this.val;
  }
}

```

Output:

> 0  
> 1 2 3 0  
> 2 0  
> 3  
> 0  
> 3 2 1 0  
> 1
