How to store multiple datatypes in an array?

I’m looking for something like an Array, but it needs to store multiple data types. The Oracle Java tutorials says, “An array is a container object that holds a fixed number of values of a single type.” So if I can’t use an array for multiple types, what do I use?

I’ve got this code that only adds one marker to the map at a time because it writes over my lat and long values each loop and only passes the last to the onPostExecute. So I will need something like an array to pass multiple forms of contact info. ie I’m pulling the location from each JSON string, but I need to pull and pass the name & phone number too to the UI from this background thread.

  • Android x86 virtualbox + camera
  • Multiple OnClick on Widget for the same Intent
  • Android — What happens when device is unlocked?
  • Connecting 2 images in android
  • How to change alert dialog header divider color android
  • Adding a new network bearer to Android
  • try {
        String apples = endpoint.listContactInfo().execute().toString();
        JSONObject jObject = new JSONObject(apples);
        JSONArray jsonArr = jObject.getJSONArray("items");
         for(int i =0 ; i<jsonArr.length() ;i++ ){
             JSONObject jsonObj1 = jsonArr.getJSONObject(i);
                        // Storing each json item in variable
                        String id = jsonObj1.getString(TAG_ID);
                        String nameFirst1 = jsonObj1.getString(TAG_FIRSTNAME);
                        String nameLast1 = jsonObj1.getString(TAG_LASTNAME);
                        String emailAddress1 = jsonObj1.getString(TAG_EMAIL);
                        String streetAddress1 = jsonObj1.getString(TAG_ADDRESS);
                        String phone1 = jsonObj1.getString(TAG_PHONE);
                        //test to see if made it to string
                        Log.d("YOUR_TAG", "First Name: " + nameFirst1 + " Last Name: " + nameLast1);
                           address = coder.getFromLocationName(streetAddress1,5);
                            Address location1 = address.get(0);
                            // SET LAT LNG VALUES FOR MARKER POINT
                         lati = location1.getLatitude();
                             longi = location1.getLongitude();
                             Log.d("Location", "Location:" + lati + " " +  longi);
        } catch (IOException e) {
      } catch (JSONException e) {
        // TODO Auto-generated catch block
          return (long) 0;
             // ADD MARKER TO MAP UI
        protected void onPostExecute(Long result) {
            mMap.addMarker(new MarkerOptions()
            .position(new LatLng(lati, longi))
             .title("Hello world"));

    Related posts:

    What are the differences among Internal storage, external storage, sd card and removable storage?
    Fragment view in ViewPager is not restored when resuming
    Android IAB - Activity destroyed after successful purchase
    Android round a Layouts background image, only top or bottom corners
    Good Tools To Develop a GUI in Android?
    Android In App Billing - Error Retrieving Information From Server
  • Crashes and ANR on Android Developer Console
  • How to change size of logcat buffer in Android?
  • How does a solo/small group of developers test on multiple devices?
  • Gallery ImageAdapter convertView is always null
  • Handling back button in React Native, Navigator on Android
  • Android: Capturing the return of an activity
  • 3 Solutions collect form web for “How to store multiple datatypes in an array?”

    You can create an array of your Custom-Class.

    public class YourCustomClass {
         String id;
         String name;
         double longitude;
         // and many more fields ...
        public YourCustomClass() {  // constructor 
        public void setID(String id) {
   = id;
        public String getID() {
            return id;
        // and many more getter and setter methods ...

    Inside your custom-class you can have as many fields as you want where you can store your data, and then use it like that:

    // with array 
    YourCustomClass [] array = new YourCustomClass[10];
    array[0] = new YourCustomClass();
    String id = array[0].getID();
    // with arraylist
    ArrayList<YourCustomClass> arraylist = new ArrayList<YourCustomClass>();
    arraylist.add(new YourCustomObject());
    String id = arraylist.get(0).getID();

    You can also let the AsyncTasks doInBackground(…) method return your Custom-class:

    protected void onPostExecute(YourCustomClass result) {
     // do stuff...

    Or an array:

    protected void onPostExecute(YourCustomClass [] result) {
     // do stuff...

    Or a ArrayList:

    protected void onPostExecute(ArrayList<YourCustomClass> result) {
     // do stuff...

    Edit: Of course, you can also make a ArrayList of your custom object.

    You can use an ArrayList.

    ArrayList<Object> listOfObjects = new ArrayList<Object>();

    And then add items to it.


    Or create your own object that encapsulates all the field that you require like

    public class LocationData {
       private double lat;
       private double longitude;
       public LocationData(double lat, double longitude) {
  = lat;
           this.longitude = longitude;

    and then add your lat/long pairs to an ArrayList of type LocationData

    ArrayList<LocationData> listOfObjects = new ArrayList<LocationData>();
    listOfObjects.add(new LocationData(lat, longitude));

    You should consider the use of the typesafe heterogeneous container pattern.

    There the data is stored in a Map<Key<?>, Object> and access to the map is hidden behind generic methods, that automatically cast the return value.

    public <T> T getObjectByKey(Key<T> key)
      return (T) map.get(key);

    The same for put.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.