# 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:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![David](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/david/32/8853_2.png) [@David](https://discourse.processing.org/u/David)
#### Post date: [April 18, 2020, 6:30pm UTC](https://discourse.processing.org/t/unable-to-append-to-an-array-of-g4p-objects/19898/1 "2020-04-18T18:30:47Z")

</div>

I can create arrays of G4P objects and access them using an index or subscript, but I don’t seem to be able to append to an array of G4P objects. I get an error message “cannot convert from Object to GPanel[]” on the line with “append()”. This works for arrays of other types of object such as Strings. Is there something special about G4P objects? I do have a reason for wanting to do this, although it’s not obvious from this stripped-down example.

Here’s my source code:

```auto
import g4p_controls.*;

public void setup() {
  size(800,600,JAVA2D);
  String[] sa = {}; 
  sa = append(sa, "MA");
  println(sa.length);
  for (int i=0; i < sa.length; i++) println(sa[i]);
  GPanel[] panels = {};
  GPanel panel = new GPanel(this,0,0,200,200);
  panels = append(panels,panel);
  println(panels.length);
}

public void draw() {
}

```

Thanks.

---

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

</div>

[Processing.org/reference/append\_.html](http://Processing.org/reference/append_.html)

---

<div class="post-metadata">

### Author: ![David](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/david/32/8853_2.png) [@David](https://discourse.processing.org/u/David)
#### Post date: [April 18, 2020, 7:13pm UTC](https://discourse.processing.org/t/unable-to-append-to-an-array-of-g4p-objects/19898/3 "2020-04-18T19:13:39Z")

</div>

Yes, that’s where I got the String example (append(sa1, “MA”)) from. My question is, why does it work for String objects but not GPanel objects?

---

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

</div>

> [@David](#):
>
> , why does it work for String objects but not GPanel objects?

It’s already been explained there:

> When using an array of objects, the data returned from the function must be cast to the object array’s data type.

---

<div class="post-metadata">

### Author: ![David](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/david/32/8853_2.png) [@David](https://discourse.processing.org/u/David)
#### Post date: [April 18, 2020, 7:25pm UTC](https://discourse.processing.org/t/unable-to-append-to-an-array-of-g4p-objects/19898/5 "2020-04-18T19:25:24Z")

</div>

But they’re the same, aren’t they? I have an array of GPanel’s, and the constructor returns a GPanel.

If this is what you mean, it doesn’t work either (same error message):

` panels = append(panels,(GPanel)panel);`

---

<div class="post-metadata">

### Author: ![David](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/david/32/8853_2.png) [@David](https://discourse.processing.org/u/David)
#### Post date: [April 18, 2020, 7:45pm UTC](https://discourse.processing.org/t/unable-to-append-to-an-array-of-g4p-objects/19898/6 "2020-04-18T19:45:10Z")

</div>

Now I understand. I changed line to the following, and it’s working now.

` panels = (GPanel[]) append(panels,panel);`

Thanks.

---

<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>
