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?

  • one
    Map can be used, with the index as a key - zRrr
  • This is ArrayList '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 a map with the Integer key instead of a List 'a with indices, or the array is int[] - it does not change its indexing. - Pavel Krizhanovskiy

1 answer 1

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 simple map without synchronization; LinkedHashMap - map without synchronization with preservation of the order of access to the elements (read / write); Hashtable - a simple map 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