# Java array with 2 types of objects

**URL:** <https://forum.codewithmosh.com/t/java-array-with-2-types-of-objects/19532>\
**Category:** Java\
**Created:** [April 12, 2023, 7:16am UTC](https://forum.codewithmosh.com/t/java-array-with-2-types-of-objects/19532 "2023-04-12T07:16:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![morten.kose](https://avatars.discourse-cdn.com/v4/letter/m/d2c977/32.png) [@morten.kose](https://forum.codewithmosh.com/u/morten.kose)\
**Post date:** [April 12, 2023, 7:16am UTC](https://forum.codewithmosh.com/t/java-array-with-2-types-of-objects/19532/1 "2023-04-12T07:16:27Z")

</div>

Hello fellow coders

Can anybody tell me how can I define this type of array?

```auto
"locations": [
              {"type": "loc", "locationId": 60},
              {"type": "map",
                  "mapLocationId": 4594,
                  "locationText": "north pole",
                  "coordX": 08.930319,
                  "coordY": 75.730848,
                  "companyId": 4
               },
               {"type": "loc", "locationId": 55},
]

```

locations array can have 2 types of multiple objects

Cheers!

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [April 27, 2023, 4:59am UTC](https://forum.codewithmosh.com/t/java-array-with-2-types-of-objects/19532/2 "2023-04-27T04:59:36Z")

</div>

There are many, many ways. The absolute simplest is just to type the array as an array of Object: `Object[]`. If you went that route you would have to case the objects as you processed them. There are other alternatives using subclasses or even interfaces. Honestly what I would do depends highly on what you need to do with that data structure. Perhaps it does not even make sense to have them in an array at all.
