# Unable to append to an array of G4P objects

**URL:** <https://discourse.processing.org/t/unable-to-append-to-an-array-of-g4p-objects/19898>\
**Category:** Libraries\
**Created:** [April 18, 2020, 6:30pm UTC](https://discourse.processing.org/t/unable-to-append-to-an-array-of-g4p-objects/19898 "2020-04-18T18:30:47Z")\
**Posts on this page:** 1\
**Showing post:** 7

<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:** [April 18, 2020, 7:50pm UTC](https://discourse.processing.org/t/unable-to-append-to-an-array-of-g4p-objects/19898/7 "2020-04-18T19:50:05Z")

</div>

Using `(cast)` is indeed not ideal. If you wish, you can override **append()** pasting the 3 methods below on your sketch, so `(cast)` isn’t needed anymore: 😉

```java
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);
}

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

```

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

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

---

_[View the full topic](https://discourse.processing.org/t/unable-to-append-to-an-array-of-g4p-objects/19898)._
