# Why i can't do operations for arrays to arrays of custom classes

**URL:** https://discourse.processing.org/t/why-i-cant-do-operations-for-arrays-to-arrays-of-custom-classes/44369
**Category:** Coding Questions
**Created:** [May 1, 2024, 6:58pm UTC](https://discourse.processing.org/t/why-i-cant-do-operations-for-arrays-to-arrays-of-custom-classes/44369 "2024-05-01T18:58:10Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![zlfp](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/zlfp/32/19543_2.png) [@zlfp](https://discourse.processing.org/u/zlfp)
#### Post date: [May 1, 2024, 6:58pm UTC](https://discourse.processing.org/t/why-i-cant-do-operations-for-arrays-to-arrays-of-custom-classes/44369/1 "2024-05-01T18:58:10Z")

</div>

shorter, there is the code we need

```auto
class creature {
 float xp,yp;
 PImage ifa;
}

class animal extends creature {
 animal(float sxp,float syp,PImage sifa) {
   xp = sxp;
   yp = syp;
   ifa = sifa;
 }
 
 void NP(float nxp,float nyp) {
   xp = nxp;
   yp = nyp;
 }
 
 void nims(PImage nifa) {
   ifa = nifa;
 }
 
}

```

suppose we need a append an array of animals

```auto
animal[] a = new animal[0];
a = append(a, new animal(0,0, somePImage))

```

but, we have an error

```auto
Type mismatch: cannot convert from Object to somefile.animal[]

```

how to fix it?  
p.s its processing 4.3 java  
p.p.s don’t ask why I need this im creating games on processing

---

<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: [May 1, 2024, 7:26pm UTC](https://discourse.processing.org/t/why-i-cant-do-operations-for-arrays-to-arrays-of-custom-classes/44369/2 "2024-05-01T19:26:55Z")

</div>

- [Why use ArrayList instead of array with append()? - Processing 2.x and 3.x Forum](https://forum.Processing.org/two/discussion/8080/why-use-arraylist-instead-of-array-with-append.html)

---

<div class="post-metadata">

### Author: ![zlfp](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/zlfp/32/19543_2.png) [@zlfp](https://discourse.processing.org/u/zlfp)
#### Post date: [May 1, 2024, 7:55pm UTC](https://discourse.processing.org/t/why-i-cant-do-operations-for-arrays-to-arrays-of-custom-classes/44369/3 "2024-05-01T19:55:56Z")

</div>

cause is not mine (its noting there in comment)

---

<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: [May 1, 2024, 8:43pm UTC](https://discourse.processing.org/t/why-i-cant-do-operations-for-arrays-to-arrays-of-custom-classes/44369/4 "2024-05-01T20:43:35Z")

</div>

Hello,

instead of

> [@zlfp](#):
>
> `a = append(a, new animal(0,0, somePImage))`

better

`a = (animal[]) append(a, new animal(0,0, somePImage))`

see: [append() / Reference / Processing.org](https://processing.org/reference/append_.html)

> When using an array of objects, the data returned from the function [append] must be cast to the object array’s data type.  
> For example: _SomeClass[] items = (SomeClass[]) append(originalArray, element)_.

or in your case: `items = (SomeClass[]) append(originalArray, element)`

- with `(SomeClass[])` being the crucial part that tells (_casts_) the Object to be of kind `animal[]`

**Remark**

Instead of just “`a`” it might be wise to name it `listOfAnimals` or `arrAnimals` or so.

Chrisir

---

<div class="post-metadata">

### Author: ![zlfp](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/zlfp/32/19543_2.png) [@zlfp](https://discourse.processing.org/u/zlfp)
#### Post date: [May 2, 2024, 10:10am UTC](https://discourse.processing.org/t/why-i-cant-do-operations-for-arrays-to-arrays-of-custom-classes/44369/5 "2024-05-02T10:10:56Z")

</div>

it works, thanx Chrisir 😁
