# Array of images

**URL:** <https://discourse.processing.org/t/array-of-images/32120>\
**Category:** Coding Questions\
**Created:** [September 6, 2021, 4:02pm UTC](https://discourse.processing.org/t/array-of-images/32120 "2021-09-06T16:02:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tati1202](https://avatars.discourse-cdn.com/v4/letter/t/9e8a1a/32.png) [@tati1202](https://discourse.processing.org/u/tati1202)\
**Post date:** [September 6, 2021, 4:02pm UTC](https://discourse.processing.org/t/array-of-images/32120/1 "2021-09-06T16:02:19Z")

</div>

PImage [] fotos= new PImage [1];  
int nroImg;  
void setup(){  
nroImg=0;  
size(700,700);  
for (int i=0; i\<fotos.length; i++){  
fotos[i]=loadImage(“mr”+i+".jpg");  
}  
}  
void draw(){  
background(0);  
image(fotos[nroImg],0, 0);  
fotos[nroImg].resize(700,700);  
println(nroImg);  
if(mousePressed==true){  
nroImg=1;  
}  
}

This is my code, and when I run it it starts, but then I click the mouse and does not load the other image, anyone has any idea why won’t the program run? By the way, both photos are .jpg and got the same name

---

<div class="post-metadata">

**Author:** ![rapatski](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rapatski/32/5164_2.png) [@rapatski](https://discourse.processing.org/u/rapatski)\
**Post date:** [September 6, 2021, 6:56pm UTC](https://discourse.processing.org/t/array-of-images/32120/2 "2021-09-06T18:56:27Z")

</div>

Hi Tati, please use the “\</\>” icon in the toolbar of the comment box to format your code.

With `new PImage[1]` you defined an array of length 1, which means that your for loop will only load 1 image. Looking at your code, pressing the mouse should actually throw an index out of bounds error, as fotos[1] will actually try and look at the second position in the array, which in your case doesn’t exist.

Try making an array of length 2?

---

<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:** [September 6, 2021, 7:06pm UTC](https://discourse.processing.org/t/array-of-images/32120/3 "2021-09-06T19:06:38Z")

</div>

> [@tati1202](#):
>
> both photos are .jpg and got the same name

I hope one is mr0.jpg, the other mr1.jpg (and not the same name)

> [@tati1202](#):
>
> PImage fotos= new PImage [1];

wild guess: `PImage [] fotos= new PImage [2]; // use 2 here`

Do you receive an Error like index out of bounds or so? It’s always important to tell us when you receive an error.

> [@tati1202](#):
>
> ```auto
> fotos[nroImg].resize(700,700);
> println(nroImg);
> 
> ```

please do this in setup() and not in draw() since it needs to be done only once

---

<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:** [September 6, 2021, 7:07pm UTC](https://discourse.processing.org/t/array-of-images/32120/4 "2021-09-06T19:07:09Z")

</div>

you beat me to it… 😉

---

<div class="post-metadata">

**Author:** ![rapatski](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rapatski/32/5164_2.png) [@rapatski](https://discourse.processing.org/u/rapatski)\
**Post date:** [September 6, 2021, 7:29pm UTC](https://discourse.processing.org/t/array-of-images/32120/5 "2021-09-06T19:29:48Z")

</div>

Tati, just for fun:

**Arrays** :  
…are just lists. Nothing fancy at all. In Java, you have to specify what ‘type’ of object this list consists of; so PImage[] means an list of images, PShape[] a list of shapes and float[] a list of floats.

When you define an array, you have to specify its length. You can make an empty array, but even then you have to say: `images = new Image[0]`.

Anyway, so the next thing is how to access items in the list. You do this by entering a number in the “access” operator, the square brackets. This number is called the index. And, here’s the confusing bit for people starting with programming, the first position in an array is at index 0.

So:

```auto
PImage[] images;

void setup() {
  images = new PImage[3]; // creates array that can hold 3 PImage objects
  images[0] = loadImage("first.jpg");
  images[1] = loadImage("second.jpg");
  images[2] = loadImage("third.jpg");
}

```

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [September 6, 2021, 9:21pm UTC](https://discourse.processing.org/t/array-of-images/32120/6 "2021-09-06T21:21:50Z")

</div>

Hi

Nice hints

[![](https://img.youtube.com/vi/xlLShhnzA14/maxresdefault.jpg "Processing Tutorial: Arrays and Images") ](https://www.youtube.com/watch?v=xlLShhnzA14&t=23s)

---

<div class="post-metadata">

**Author:** ![tati1202](https://avatars.discourse-cdn.com/v4/letter/t/9e8a1a/32.png) [@tati1202](https://discourse.processing.org/u/tati1202)\
**Post date:** [September 9, 2021, 8:48pm UTC](https://discourse.processing.org/t/array-of-images/32120/7 "2021-09-09T20:48:20Z")

</div>

thanks to all of you guys, I had a bad thinking about the numer [] here.
