Use Map<Integer,Integer>
. The key will be your index, and the value will be the value of the list.
For your sub-system requirement, perhaps TreeMap<Integer,Integer>
will work, as it preserves the sorting of keys and simplifies iteration over the sub-list.
Of course, this means that you cannot use the List
interface. If you must use the List
interface, you can create your own List
implementation supported by TreeMap
(for example, list.add(5,3)
will call map.put(5,3)
).
source share