2
我試圖將數據添加到數組列表中。在這個結果如何獲取Java列表中的對象索引
[{Store={id_store=2, namestore=Kupat Tahu Singaparna , product=[{id_product=1, price=100000, quantity=1}]}}]
,你可以使用此代碼:
static ArrayList Storecart = new ArrayList();
LinkedHashMap store = new LinkedHashMap();
LinkedHashMap storeitem = new LinkedHashMap();
LinkedHashMap listproduct = new LinkedHashMap();
ArrayList prdct = new ArrayList();
storeitem.put("id_store",id_store);
storeitem.put("namestore",namestr);
listproduct.put("id_product", id_prodt);
listproduct.put("price",priced);
listproduct.put("quantity", quantity);
prdct.add(listproduct);
storeitem.put("product", prdct);
store.put("Store", storeitem);
Storecart.add(store);
我需要得到一個對象的索引的數組列表。問題是,我無法循環訪問「獲取對象存儲和對象產品」的數組列表,並找到每個索引。什麼是最佳的有效解決方案?
爲什麼你在這裏使用'Map',當你應該寫一個類來保存這些數據?並且_why_不能循環訪問'ArrayList'嗎? (最後,爲什麼不使用泛型,這會讓你的代碼更具可讀性?) –
尋找List.indexOf(Object)? – PermGenError
['ArrayList'有一個'indexOf'方法](http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#indexOf(java.lang.Object))。 –