java - Accessing specific ArrayList elements, when the ArrayList changes frequently -
i have arraylist
stores object foo
.
let's want add 10 of objects foo
arraylist
, following:
for (int = 0; <10 ; i++){ arraylist.add(new foo()); }
while solution works, problem is, how access, ninth foo, after third , fourth foo has been deleted away arraylist?
i cannot do
arraylist.get(9);
anymore, since throw indexoutofboundsexception
.
i want able make program dynamically, since in case number of foos not fixed, , can deleted @ time.
you need have items in specific order , may not remember foo inserted @ index, map<integer, foo> perfect.
map<integer,foo> map = new hashmap<integer, foo>(); map.put(1, new foo()); map.put(2, new foo()); etc
per comments, map = treemap<integer, foo>()
might better.
Comments
Post a Comment