# Using arrays inside for loops

**URL:** <https://discourse.processing.org/t/using-arrays-inside-for-loops/481>\
**Category:** Coding Questions\
**Created:** [May 29, 2018, 12:45am UTC](https://discourse.processing.org/t/using-arrays-inside-for-loops/481 "2018-05-29T00:45:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Focksbot](https://avatars.discourse-cdn.com/v4/letter/f/ea5d25/32.png) [@Focksbot](https://discourse.processing.org/u/Focksbot)\
**Post date:** [May 29, 2018, 12:45am UTC](https://discourse.processing.org/t/using-arrays-inside-for-loops/481/1 "2018-05-29T00:45:36Z")

</div>

Pretty simple question, I hope. Why does this first piece of code draw three lovely mountains, while the second does nothing?

First bit:

```auto
void setup(){
	size(800,420); //size of mountains
}

int array[0] = 0;
int array[1] = 1;
int array[2] = 2;
int array[3] = 3;

void draw(){
	background(255);
	for(int i=0; i<3; i++){
		for(float x=1; x<width; x++){
			int a = i;
			stroke(200-a*70); // color
			line(x,height,x,height-noise(10*i+x/width)*(height-50*i));
		}
	}
}

```

Second bit:

```auto
void setup(){
	size(800,420); //size of mountains
}

int array[0] = 0;
int array[1] = 1;
int array[2] = 2;
int array[3] = 3;

void draw(){
	background(255);
	for(int i=0; i<3; i++){
		for(float x=1; x<width; x++){
			int a = array[i];
			stroke(200-a*70); // color
			line(x,height,x,height-noise(10*i+x/width)*(height-50*i));
		}
	}
}

```

The difference is that in the second, I’m trying to get the same numerical values out of positions in an array.

---

<div class="post-metadata">

**Author:** ![colouredmirrorball](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/colouredmirrorball/32/234_2.png) [@colouredmirrorball](https://discourse.processing.org/u/colouredmirrorball)\
**Post date:** [May 29, 2018, 12:55am UTC](https://discourse.processing.org/t/using-arrays-inside-for-loops/481/2 "2018-05-29T00:55:16Z")

</div>

I don’t even know how you got the second one to compile as it is completely wrong. It does not compile on my computer. It even tells me to “Delete this”.

An array should be used like this:

```auto
int array[] = new int[4];
array[0] = 0;
array[1] = 1;
//...

```

or if you want, like this:

`int[] array = new int[4];`

(these lines are equivalent).

Or, if you like shorthand:

`int[] array = {0,1,2,3};`

See:  
[https://processing.org/reference/Array.html](https://processing.org/reference/Array.html)  
[https://processing.org/reference/arrayaccess.html](https://processing.org/reference/arrayaccess.html)

---

<div class="post-metadata">

**Author:** ![Focksbot](https://avatars.discourse-cdn.com/v4/letter/f/ea5d25/32.png) [@Focksbot](https://discourse.processing.org/u/Focksbot)\
**Post date:** [May 29, 2018, 12:59am UTC](https://discourse.processing.org/t/using-arrays-inside-for-loops/481/3 "2018-05-29T00:59:42Z")

</div>

> [@colouredmirrorball](#):
>
> int array = {0,1,2,3};

Thanks very much. Thought that was a legit alternative way to set up the array, but obviously not!

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [May 29, 2018, 1:00am UTC](https://discourse.processing.org/t/using-arrays-inside-for-loops/481/4 "2018-05-29T01:00:45Z")

</div>

> [@What means RawY\[RawY.length-1\]](https://discourse.processing.org/t/what-means-rawy-rawy-length-1/341/3):
>
> Shorter answer: angel A Java array got a field called length, which keeps the number of indexed elements it contains. Its 1st index, a.K.a the head element, is 0. Its last index, a.K.a. the tail element, is length - 1. We specify the index we wish to access via the square brackets [] operator: [https://Processing.org/reference/arrayaccess.html](https://Processing.org/reference/arrayaccess.html) For further details: [http://www.TutorialsPoint.com/java/java\_arrays.htm](http://www.TutorialsPoint.com/java/java_arrays.htm)

---

<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:** [May 29, 2018, 5:22pm UTC](https://discourse.processing.org/t/using-arrays-inside-for-loops/481/5 "2018-05-29T17:22:31Z")

</div>

@Focksbot:

To stay organized, first declare globals and optionally assign values at the same time (on the same line).

So you cannot do this:

```auto
int x;
x = 2; // illegal
void setup(){
  print(x);
}

```

…but you can assign globally at declaration, or declare, then assign later:

```auto
int x = 2;
int y;
void setup(){
  y = 4;
  print(x, y);
}

```

Arrays work the same way. That means you can do any of these things:

```auto
int a[] = new int[2]; // size now, values later
int b[]; // size later
int c[] = { 10, 20 }; // values now, sets the size
int d[] = { 30, 40 };
int e[] = { 40, 50 };
void setup(){
  a[0] = 10;
  a[1] = 20;
  b = new int[2]; // must size first!
  b[0] = 100;
  b[1] = 200;
  c[0] = 999; // change a value
  d = new int[4]; // replace with new size (empty)
  e = new int[]{ 0, 1, 2, 3}; // replace with new values, different size
}

```
