How to store multiple datatypes in an array?

后端 未结 3 1604
一向
一向 2021-01-02 05:48

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 numb

3条回答
  •  离开以前
    2021-01-02 06:22

    You can use an ArrayList.

    ArrayList listOfObjects = new ArrayList();
    
    
    

    And then add items to it.

    listOfObjects.add("1");
    listOfObjects.add(someObject);
    

    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) {
           this.lat = lat;
           this.longitude = longitude;
       }
       //getters
       //setters
    }
    

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

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

    提交回复
    热议问题