Java array with 2 types of objects

Hello fellow coders

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

"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


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.