Reputation: 53
I'm trying to add data into array list. in this result
[{Store={id_store=2, namestore=Kupat Tahu Singaparna , product=[{id_product=1, price=100000, quantity=1}]}}]
you could use this code:
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);
I need to get the index of an object in the array list. The problem is, I can't looping array list for "get object Store, and object product" and find every index.. what will be the best & efficient solution ?
Upvotes: 2
Views: 284
Reputation: 427
Well, you could use List.indexOf(), as others have suggested:
If you plan on doing this a lot, then presumably you have a handle on your Object reference. So, you could just extend/wrap the Object you want the index of to keep track of its own index. Specifically, you could assign the Object its index based on the order you first encounter it or something. The order of the Object in the Collection would then be irrelevant.
I suppose you could also use a Map as yet another possibility. Then only work with the Map instead of an ArrayList.
Bottom-line: if you're doing a lot of "indexOf()" requests, then the ArrayList may not be the best container for your data.
Upvotes: 1