# I can't add stuff to my array

**URL:** <https://discourse.processing.org/t/i-cant-add-stuff-to-my-array/18772>\
**Category:** Beginners\
**Created:** [March 18, 2020, 6:45pm UTC](https://discourse.processing.org/t/i-cant-add-stuff-to-my-array/18772 "2020-03-18T18:45:16Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![alkilum](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@alkilum](https://discourse.processing.org/u/alkilum)\
**Post date:** [March 18, 2020, 6:45pm UTC](https://discourse.processing.org/t/i-cant-add-stuff-to-my-array/18772/1 "2020-03-18T18:45:16Z")

</div>

I have an short[] array, and i want to add a bunch of 0s to it.

Here is my code:

```auto
size(800,600);
short [] visible;
for(int n=0;n<2985984;n+=1) {
    visible.add(0);
}

```

I get an error saying “Cannot invoke add(int) an the array type short[]”  
I also tried making a short called stayas0 and in the for loop doing visible.add(stayas0); instead, but to no avail. anyone know why it’s not working?

---

<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:** [March 18, 2020, 6:48pm UTC](https://discourse.processing.org/t/i-cant-add-stuff-to-my-array/18772/2 "2020-03-18T18:48:56Z")

</div>

hello!

And welcome to the community!

Nice to have your here!

`add()` is for ArrayList

for an array please use append

String[] sa2 = append(sa1, “MA”);

[https://www.processing.org/reference/append\_.html](https://www.processing.org/reference/append_.html)

---

<div class="post-metadata">

**Author:** ![alkilum](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@alkilum](https://discourse.processing.org/u/alkilum)\
**Post date:** [March 18, 2020, 6:50pm UTC](https://discourse.processing.org/t/i-cant-add-stuff-to-my-array/18772/3 "2020-03-18T18:50:18Z")

</div>

Thanks!

---

<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:** [March 18, 2020, 6:50pm UTC](https://discourse.processing.org/t/i-cant-add-stuff-to-my-array/18772/4 "2020-03-18T18:50:37Z")

</div>

And welcome to the community!

---

<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:** [March 18, 2020, 6:57pm UTC](https://discourse.processing.org/t/i-cant-add-stuff-to-my-array/18772/5 "2020-03-18T18:57:52Z")

</div>

I am sorry, it doesn’t work with short[] somehow.

here is a Sketch with int[]

```auto

size(800, 600);

int[] visible = new int[0];

for (int n=0; n<12; n+=1) {
  visible = (int[]) append(visible, 10);
}

printArray(visible);

```

---

<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:** [March 29, 2020, 4:37am UTC](https://discourse.processing.org/t/i-cant-add-stuff-to-my-array/18772/6 "2020-03-29T04:37:25Z")

</div>

> [@alkilum](#):
>
> ```auto
> short [] visible;
> for(int n=0;n<2985984;n+=1) {
> // ...
> }
> 
> ```

Note that looping on a fixed length array and resizing it each time is a bad idea. You are reallocating it 2985984 times.

Instead, do this:

```auto
short [] visible = new short[2985984];
for(int n=0; n<visible.length; n+=1) {
  // ...
}

```

Or if for some reason you don’t know how long it will eventually (its length is dynamically determined by some process that you can’t solve before the loop begins) – then use ArrayList or IntList.

---

<div class="post-metadata">

**Author:** ![alkilum](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@alkilum](https://discourse.processing.org/u/alkilum)\
**Post date:** [August 2, 2020, 1:27am UTC](https://discourse.processing.org/t/i-cant-add-stuff-to-my-array/18772/7 "2020-08-02T01:27:29Z")

</div>

Yeah, I fixed the reallocation problem. Although short double long and other memory based data types are pretty glitchy.

---

<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 2, 2020, 2:43am UTC](https://discourse.processing.org/t/i-cant-add-stuff-to-my-array/18772/8 "2020-08-02T02:43:28Z")

</div>

Glad it worked for you.

> [@alkilum](#):
>
> short double long and other memory based data types are pretty glitchy

glitchy how? What problem are you having?

---

<div class="post-metadata">

**Author:** ![alkilum](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@alkilum](https://discourse.processing.org/u/alkilum)\
**Post date:** [August 2, 2020, 4:21am UTC](https://discourse.processing.org/t/i-cant-add-stuff-to-my-array/18772/9 "2020-08-02T04:21:25Z")

</div>

It’s been a while since I last used them; I think I remember them just generally not working; like I would try to set them and it wouldn’t work. I should probably check the reference though.

---

<div class="post-metadata">

**Author:** ![alkilum](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@alkilum](https://discourse.processing.org/u/alkilum)\
**Post date:** [August 2, 2020, 4:32am UTC](https://discourse.processing.org/t/i-cant-add-stuff-to-my-array/18772/10 "2020-08-02T04:32:53Z")

</div>

Also see chrisir’s comment on how it “doesn’t work with short[]”. I think it was just kind of neglected because it’s not used very much cause we got tons of memory nowadays.

---

<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 2, 2020, 4:48am UTC](https://discourse.processing.org/t/i-cant-add-stuff-to-my-array/18772/11 "2020-08-02T04:48:38Z")

</div>

Got it. Sure, the Processing API has many places where methods aren’t implemented that use data types other than int() and float(). If you try to use something with double or short without conversion, it will fail. On the other hand, primitive arrays in and of themselves are not glitchy – in the sense of having bugs or inconsistencies that need to be ironed out. They are in the bones of the Java language and have been for decades, so they are rock solid, such as they are.
