There is an ArrayList, add (Index, Object) is used, when I delete an item (by index), the indexes in the ArrayList are shifted and I lose the index and cannot access it. How to solve a problem?
|
1 answer
If you want to use an ArrayList, you can not delete the element, but replace it with null - set (int index, null). Thus the index will remain.
- On null? Interesting, I'll try - Denis Kotlyarov
- I think I need to use a map, but which map? without synchronization - Denis Kotlyarov
HashMap
- a simplemap
without synchronization;LinkedHashMap
-map
without synchronization with preservation of the order of access to the elements (read / write);Hashtable
- a simplemap
with synchronization; If you say in more detail what you are trying to implement, then it will be possible to specifically answer your question "but which map?". - Pavel Krizhanovskiy
|
Map
can be used, with the index as a key - zRrrArrayList
's correct work, and you shouldn’t try to make it work differently. Can you explain exactly what you are trying to implement? If elements are rigidly tied to their indices, you need to use amap
with theInteger
key instead of aList
'a with indices, or the array isint[]
- it does not change its indexing. - Pavel Krizhanovskiy