# Increase the length of object array

**URL:** https://discourse.processing.org/t/increase-the-length-of-object-array/5390
**Category:** Coding Questions
**Created:** [November 9, 2018, 7:53pm UTC](https://discourse.processing.org/t/increase-the-length-of-object-array/5390 "2018-11-09T19:53:43Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![dragon\_hatcher](https://avatars.discourse-cdn.com/v4/letter/d/46a35a/32.png) [@dragon\_hatcher](https://discourse.processing.org/u/dragon_hatcher)
#### Post date: [November 9, 2018, 7:53pm UTC](https://discourse.processing.org/t/increase-the-length-of-object-array/5390/1 "2018-11-09T19:53:43Z")

</div>

Hello Everyone,

I’m just learning to use classes and I wanted to use an array to create the objects. I have it set up so that when I click it creates a new object at the mouses position. What I am wondering is how I can increase the length of the array so that I can always add another object. I couldn’t get expand to work

Thanks

My Code:

```auto
TestClass[] test = new TestClass[1];
int called = 0;

void setup() {
  size(800, 801);
  fill(#000000);
}

void draw() {
  background(#ffffff);
  for(int i = 0; i < called; i++){
  test[i].update();
  test[i].doDraw();
  }
}

void mousePressed(){
  
  test = expand(test, test.length + 1);
  test[called] = new TestClass(5, mouseX, mouseY);  
  called += 1;
  
}

class TestClass {
  int y, x, upDown;
  
  TestClass(int in, int x1, int y1){
  y = y1;
  x = x1;
  upDown = in;
  }
  
  void update() {
    if (y <= 0) {
      upDown = upDown * -1;
    } else if (y >= height) {
      upDown = upDown * -1;
    }
    y += upDown;
  }
  
  void doDraw(){
    fill(#000000);
    ellipse(x, y, 10, 10);
  }
}

```

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 9, 2018, 8:12pm UTC](https://discourse.processing.org/t/increase-the-length-of-object-array/5390/2 "2018-11-09T20:12:18Z")

</div>

Please Format your code correctly. You can do so by pressing Strg+T in Processing and copying it in the place created by the \</\> sign When Sending the Post/ editing it.

And why don‘t you just set called to 0?  
And what didn‘t work? Everything seems right to me…

---

<div class="post-metadata">

### Author: ![dragon\_hatcher](https://avatars.discourse-cdn.com/v4/letter/d/46a35a/32.png) [@dragon\_hatcher](https://discourse.processing.org/u/dragon_hatcher)
#### Post date: [November 9, 2018, 8:19pm UTC](https://discourse.processing.org/t/increase-the-length-of-object-array/5390/3 "2018-11-09T20:19:21Z")

</div>

Alright, I did that.

Not really sure what I was thinking when I set called to 1

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 9, 2018, 8:22pm UTC](https://discourse.processing.org/t/increase-the-length-of-object-array/5390/4 "2018-11-09T20:22:01Z")

</div>

Ok, test = expand(test, test.length+1) seemed better, to just add one free space for each Single Addition, not double the size each Time one gets added.

And you Should set the first testClass in setup, to avoid Setting it in mousePressed. And then use called+1 in mousePressed to set the new object, and not the one created before. Could cause problems later on.

---

<div class="post-metadata">

### Author: ![dragon\_hatcher](https://avatars.discourse-cdn.com/v4/letter/d/46a35a/32.png) [@dragon\_hatcher](https://discourse.processing.org/u/dragon_hatcher)
#### Post date: [November 9, 2018, 8:24pm UTC](https://discourse.processing.org/t/increase-the-length-of-object-array/5390/5 "2018-11-09T20:24:21Z")

</div>

Ya, I did have that, it still gives me an error though

"Type mismatch, “java.lang.Object” does not match with “class\_test.TestClass[]”

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 9, 2018, 8:26pm UTC](https://discourse.processing.org/t/increase-the-length-of-object-array/5390/6 "2018-11-09T20:26:08Z")

</div>

Ah, ok. Thats probably because the expand method only accepts processings base classes… just like sort does…But it‘s easy to create it yourself. Just make a new method with ref to the array, then in the method, create a new array of testClass with size of array.length +1. Then copy the array into the new array and create the new Element.

---

<div class="post-metadata">

### Author: ![dragon\_hatcher](https://avatars.discourse-cdn.com/v4/letter/d/46a35a/32.png) [@dragon\_hatcher](https://discourse.processing.org/u/dragon_hatcher)
#### Post date: [November 9, 2018, 8:27pm UTC](https://discourse.processing.org/t/increase-the-length-of-object-array/5390/7 "2018-11-09T20:27:25Z")

</div>

You mean create a function that just duplicates the list with one extra item. I think I could do that thanks

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 9, 2018, 8:29pm UTC](https://discourse.processing.org/t/increase-the-length-of-object-array/5390/8 "2018-11-09T20:29:25Z")

</div>

Oh Yeah… just explained it anyway 😅

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 9, 2018, 8:32pm UTC](https://discourse.processing.org/t/increase-the-length-of-object-array/5390/9 "2018-11-09T20:32:44Z")

</div>

Or actually, have a Look at ArrayLists. They are pretty handy because they have Dynamic length (Or so). But i don‘t know if they work with new classes like expand doesn‘t.

---

<div class="post-metadata">

### Author: ![Jakub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jakub/32/2192_2.png) [@Jakub](https://discourse.processing.org/u/Jakub)
#### Post date: [November 11, 2018, 2:05pm UTC](https://discourse.processing.org/t/increase-the-length-of-object-array/5390/10 "2018-11-11T14:05:52Z")

</div>

You need to cast the array to your type like this:

```auto
test = (TestClass[]) expand(test, newlength);

```

Resizing the array one element at a time will be slow. Usually the best idea is to double the size and keep the number of elements in the array in an int. Or as @Lexyth suggested, use ArrayList, it resizes automatically.

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 11, 2018, 3:54pm UTC](https://discourse.processing.org/t/increase-the-length-of-object-array/5390/11 "2018-11-11T15:54:24Z")

</div>

Oh, didn‘t know that worked 😅 Yeah, then that‘s better than changing it‘s length manually.

---

<div class="post-metadata">

### Author: ![Jakub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jakub/32/2192_2.png) [@Jakub](https://discourse.processing.org/u/Jakub)
#### Post date: [November 11, 2018, 5:32pm UTC](https://discourse.processing.org/t/increase-the-length-of-object-array/5390/12 "2018-11-11T17:32:04Z")

</div>

Reading the reference pays off 🙂

[https://processing.org/reference/expand\_.html](https://processing.org/reference/expand_.html)

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 12, 2018, 4:42pm UTC](https://discourse.processing.org/t/increase-the-length-of-object-array/5390/13 "2018-11-12T16:42:41Z")

</div>

Well Yeah, But i already read it whole once… just that it‘s way too much to keep everything in mind 😅

---

<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: [November 12, 2018, 10:23pm UTC](https://discourse.processing.org/t/increase-the-length-of-object-array/5390/14 "2018-11-12T22:23:20Z")

</div>

> [@Jakub](#):
>
> You need to `(cast)` the array to your type like this:

Pasting this modified **expand()** to a sketch, there’s no more need to use `(cast)` over the returned array: 🤩

> <https://github.com/GoToLoop/processing/blob/672a8793dc19ff3e6b02bb745ae23d05155c9a66/core/src/processing/core/PApplet.java#L8360-L8366>

P.S.: The overridden **expand()** method needs: `import java.util.Arrays;` in order to compile.

Alternatively you can prefix `java.util.` to `Arrays.copyOf(list, newSize)`:  
`return java.util.Arrays.copyOf(list, newSize);`

---

<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: [November 12, 2018, 10:47pm UTC](https://discourse.processing.org/t/increase-the-length-of-object-array/5390/15 "2018-11-12T22:47:08Z")

</div>

Given **expand()** is invoked to merely increase _length_ by just 1, you can use **append()** instead: 💡  
[Processing.org/reference/append\_.html](http://Processing.org/reference/append_.html)

And override the original **append()** w/ my own version of it for a cleaner code w/o `(cast)`: 😇

> <https://github.com/GoToLoop/processing/blob/672a8793dc19ff3e6b02bb745ae23d05155c9a66/core/src/processing/core/PApplet.java#L8464-L8468>

Some modifications I did to your original sketch in order to use the customized **append()**: 😉

```auto
TestClass[] test = {};

void setup() {
  size(800, 600);
  fill(0);
}

void draw() {
  background(-1);

  for (final TestClass t : test) {
    t.update();
    t.doDraw();
  }
}

void mousePressed() {
  test = append(test, new TestClass(5, mouseX, mouseY));
}

static final <T> T[] append(T array[], final T value) {
  array = expand(array, array.length + 1);
  array[array.length - 1] = value;
  return array;
}

static final <T> T[] expand(final T list[]) {
  return expand(list, list.length << 1);
}

static final <T> T[] expand(final T list[], final int newSize) {
  return java.util.Arrays.copyOf(list, newSize);
}

```

P.S.: Just paste your class TestClass to the refactored code above to have a complete running sketch. 🤓
