Hashmap的Arraylist替换特定索引

时间:2014-12-11 05:18:40

标签: java android arraylist replace hashmap

您好我正在尝试替换包含hashmap的arraylist的特定索引。这是我试图获取它的代码,

 ArrayList<HashMap<String, String>> vehiclehistory = new ArrayList<HashMap<String, String>>();
 HashMap<String, String> map = new HashMap<String, String>();

向地图和arraylist添加值,

 for (int i = 0; i < user.length(); i++) {
 map.put(TAG_Latitude + i, latitude);
 map.put(TAG_Longitude + i, longitude);
 map.put(TAG_Speed + i, speed);
 map.put(TAG_Vehicle_Status + i, sleepstring);
 vehiclehistory.add(i, map);
 }

我希望在特定索引处添加单个地图,例如

map adding::{address0=965, ECR Link Road, VGP Prabhu Nagar, Medavakkam, Chennai, Tamil Nadu 600100, India, latitude0=12.906466666667, longitude0=80.204440000000, speed0=0, bus_tracking_timestamp0=2014-12-08 10:36:01, vehicle_status0=1, exceed_speed_limit0=0}

map adding::{address1=965, ECR Link Road, VGP Prabhu Nagar, Medavakkam, Chennai, Tamil Nadu 600100, India, latitude1=12.906466666667, longitude1=80.204440000000, speed1=0, bus_tracking_timestamp1=2014-12-08 10:36:01, vehicle_status1=1, exceed_speed_limit1=0}

但它添加了,

map adding::{exceed_speed_limit1=0, exceed_speed_limit0=0, address0=965, ECR Link Road, VGP 
Prabhu Nagar, Medavakkam, Chennai, Tamil Nadu 600100, India, address1=965, ECR Link Road, VGP  
Prabhu Nagar, Medavakkam, Chennai, Tamil Nadu 600100, India, bus_tracking_timestamp1=2014-12-08 
10:36:32, bus_tracking_timestamp0=2014-12-08 10:36:01, latitude0=12.906466666667, 
latitude1=12.906466666667, vehicle_status1=1, speed1=0, longitude0=80.204440000000, 
longitude1=80.204440000000, speed0=0, vehicle_status0=1}

地图正在附加,我的arraylist看起来像,

 12-11 10:16:15.291: I/System.out(28913): history list val::[{exceed_speed_limit1=0, 
 exceed_speed_limit0=0, address0=965, ECR Link Road, VGP Prabhu Nagar, Medavakkam, Chennai, Tamil   
 Nadu 600100, India, address1=965, ECR Link Road, VGP Prabhu Nagar, Medavakkam, Chennai, Tamil 
 00100, India, bus_tracking_timestamp1=2014-12-08 10:36:32, bus_tracking_timestamp0=2014-12-08 
 10:36:01, latitude0=12.906466666667, latitude1=12.906466666667, vehicle_status1=1, speed1=0, 
 longitude0=80.204440000000, longitude1=80.204440000000, speed0=0, vehicle_status0=1},
 {exceed_speed_limit1=0, exceed_speed_limit0=0, address0=965, ECR Link Road, VGP Prabhu Nagar,
 Medavakkam, Chennai, Tamil Nadu 600100, India, address1=965, ECR Link Road, VGP Prabhu Nagar,
 Medavakkam, Chennai, Tamil Nadu 600100, India, bus_tracking_timestamp1=2014-12-08 10:36:32, 
 bus_tracking_timestamp0=2014-12-08 10:36:01, latitude0=12.906466666667,     
 latitude1=12.906466666667, vehicle_status1=1, speed1=0, longitude0=80.204440000000,  
 longitude1=80.204440000000, speed0=0,vehicle_status0=1}]

任何人都可以告诉如何将hashmap值专门添加到arraylist并用特定的某些地图值替换arraylist的特定索引吗?

4 个答案:

答案 0 :(得分:1)

在数组列表中添加所有数据后,您想要更改特定的索引,请尝试此操作。

ArrayList<HashMap<String, String>> vehiclehistory = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map = vehiclehistory.get(index);
map.put("key_name", "string");

答案 1 :(得分:1)

您每次都会向map添加相同的ArrayList。您需要创建一个新的HashMap实例并在每次迭代时填充它,如下所示:

HashMap<String, String> map = null;

for (int i = 0; i < user.length(); i++) {
    map = new HashMap<String, String>();

    map.put(TAG_Latitude + i, latitude);
    map.put(TAG_Longitude + i, longitude);
    map.put(TAG_Speed + i, speed);
    map.put(TAG_Vehicle_Status + i, sleepstring);

    vehiclehistory.add(i, map);
}

答案 2 :(得分:1)

您只创建了一次hashmap并将相同的hashmap添加到导致问题的ararylist中,如下所示: -

 ArrayList<HashMap<String, String>> vehiclehistory = new ArrayList<HashMap<String, String>>();
 HashMap<String, String> map =null;

for (int i = 0; i < user.length(); i++) {
map = new HashMap<String, String>();

map.put(TAG_Latitude + i, latitude);
map.put(TAG_Longitude + i, longitude);
map.put(TAG_Speed + i, speed);
map.put(TAG_Vehicle_Status + i, sleepstring);
vehiclehistory.add(i, map);
}

答案 3 :(得分:1)

试试这个......

声明为全局变量

Map<String, String> myLatLongHashMap = new HashMap<String, String>();
int k = 0; 

分配Hashmap值

    myLatLongHashMap.put("lat" + k, latvalue);
    myLatLongHashMap.put("long" + k, longvalue);
    myLatLongHashMap.put("TAG_Speed" + k, TAG_Speed_value+"");
    k++;

检索Hashmap值

  if (myLatLongHashMap.size() > 0)
  {
    for (int i = 0; i < myLatLongHashMap.size() / 3; i++)
    {
         // retrieve your lat, long value..
      Double lat   =   Double.valueOf(myLatLongHashMap.get("lat" + i)); 
      Double longg  =   Double.valueOf(myLatLongHashMap.get("long" + i));
      String speed =   myLatLongHashMap.get("TAG_Speed" + i);
    }
  }