如何存储多个数据类型的数组? [英] How to store multiple datatypes in an array?

查看:387
本文介绍了如何存储多个数据类型的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在找东西像一个数组,但它需要存储多种数据类型。在Oracle Java教程说,数组是保存用于单个类型的值的一个固定数目的容器对象。所以,如果我不能使用数组的多种类型的,我用什么?

我这有code表示只会增加一个标记到地图的时间,因为它写在我的纬度和长值的每个循环,只有通过最后的onPostExecute。所以,我需要像一个数组来传递各种形式的联系信息。也就是说,我拉着从每个JSON字符串的位置,但我需要拉并通过名称和放大器;电话号码也从该后台线程的用户界面。

 尝试{

    。字符串苹果= endpoint.listContactInfo()执行()的toString()。

    JSONObject的jObject =新的JSONObject(苹果);

    JSONArray jsonArr = jObject.getJSONArray(项目);

     的for(int i = 0; I< jsonArr.length();我++){
         的JSONObject jsonObj1 = jsonArr.getJSONObject(ⅰ);


                    //保存在变量中的每个JSON项目
                    字符串ID = jsonObj1.getString(TAG_ID);
                    字符串nameFirst1 = jsonObj1.getString(TAG_FIRSTNAME);
                    字符串nameLast1 = jsonObj1.getString(TAG_LASTNAME);
                    字符串emailAddress1 = jsonObj1.getString(TAG_EMAIL);
                    字符串streetAddress1 = jsonObj1.getString(TAG_ADDRESS);
                    字符串PHONE1 = jsonObj1.getString(TAG_PHONE);

                    //测试,看看去到字符串
                    Log.d(YOUR_TAG,名字:+ nameFirst1 +姓+ nameLast1);

                       地址= coder.getFromLocationName(streetAddress1,5);

                        地址LOCATION1 = address.get(0);

                        //设置LAT LNG价值的标记点

                     LATI = location1.getLatitude();
                         隆基= location1.getLongitude();



                         Log.d(位置,位置:+ LATI ++隆基);


     }

    }赶上(IOException异常E){
    e.printStackTrace();
  }赶上(JSONException E){
    // TODO自动生成的catch块
    e.printStackTrace();
}
      返回(长)0;


    }
         //增加标记地图UI
    保护无效onPostExecute(长效){
        mMap.addMarker(新MarkerOptions()
        .position(新经纬度(LATI,隆基))
         .title伪(的Hello world));
    }
 

解决方案

您可以创建一个阵列的定制类。

 公共类YourCustomClass {

     字符串ID;
     字符串名称;
     双经度;
     //等等领域...

    公共YourCustomClass(){//构造

    }

    公共无效SETID(字符串ID){
        this.id = ID;
    }

    公共字符串的getID(){
        返回ID;
    }

    //还有更多getter和setter方法​​...
}
 

在自定义类,只要你想,你可以有很多领域,你可以存储你的数据,然后用它这样的:

  //用数组
YourCustomClass []数组=新YourCustomClass [10];
数组[0] =新YourCustomClass();
数组[0] .setID(yourid);

字符串ID =数组[0] .getID();

//使用数组列表
ArrayList的< YourCustomClass> ArrayList的=新的ArrayList< YourCustomClass>();
arraylist.add(新YourCustomObject());
arraylist.get(0).setID(yourid);

串的id = arraylist.get(0).getID();
 

您还可以让AsyncTasks doInBackground(...)方法返回您的自定义类:

 保护无效onPostExecute(YourCustomClass结果){
 //做的东西...
}
 

或数组:

 保护无效onPostExecute(YourCustomClass []的结果){
 //做的东西...
}
 

或者ArrayList的:

 保护无效onPostExecute(ArrayList中< YourCustomClass>的结果){
 //做的东西...
}
 

编辑:当然,你也可以让你的自定义对象的的ArrayList

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.

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"));
    }  

解决方案

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:

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.

这篇关于如何存储多个数据类型的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆