如何在数组中存储多个数据类型?

时间:2022-04-14 15:41:42

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?

我在寻找类似数组的东西,但是它需要存储多个数据类型。Oracle Java教程说,“数组是一个容器对象,它包含单个类型的固定数量的值。”如果我不能对多个类型使用数组,我要用什么?

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.

我有这个代码,每次只向map添加一个标记,因为它在我的lat和long值上写每个循环,并且只将最后一个标记传递给onPostExecute。所以我需要一个数组来传递多种形式的联系信息。我正在从每个JSON字符串中提取位置,但是我需要从这个后台线程中将名称和电话号码也拖放到UI中。

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) {
    e.printStackTrace();
  } catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
      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"));
    }  

3 个解决方案

#1


6  

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) {
        this.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();
array[0].setID("yourid");

String id = array[0].getID();

// with arraylist
ArrayList<YourCustomClass> arraylist = new ArrayList<YourCustomClass>();
arraylist.add(new YourCustomObject());
arraylist.get(0).setID("yourid");

String id = arraylist.get(0).getID();

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

您还可以让AsyncTasks doInBackground(…)方法返回您的定制类:

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

Or an array:

或数组:

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

Or a ArrayList:

或一个ArrayList:

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

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

编辑:当然,您也可以创建自定义对象的数组列表。

#2


10  

You can use an ArrayList.

您可以使用ArrayList。

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

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

然后将lat/long对添加到类型LocationData的ArrayList中

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

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

#3


2  

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

您应该考虑使用typesafe异构容器模式。

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.

数据存储在Map 、对象>和对映射的访问隐藏在泛型方法后面,这些方法自动转换返回值。 >

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

The same for put.

相同的。

#1


6  

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) {
        this.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();
array[0].setID("yourid");

String id = array[0].getID();

// with arraylist
ArrayList<YourCustomClass> arraylist = new ArrayList<YourCustomClass>();
arraylist.add(new YourCustomObject());
arraylist.get(0).setID("yourid");

String id = arraylist.get(0).getID();

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

您还可以让AsyncTasks doInBackground(…)方法返回您的定制类:

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

Or an array:

或数组:

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

Or a ArrayList:

或一个ArrayList:

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

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

编辑:当然,您也可以创建自定义对象的数组列表。

#2


10  

You can use an ArrayList.

您可以使用ArrayList。

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

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

然后将lat/long对添加到类型LocationData的ArrayList中

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

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

#3


2  

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

您应该考虑使用typesafe异构容器模式。

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.

数据存储在Map 、对象>和对映射的访问隐藏在泛型方法后面,这些方法自动转换返回值。 >

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

The same for put.

相同的。