I am currently using an ArrayList to store a list of elements, as a result of which I will need to insert new elements at specific positions. I need me to enter elements in a position greater than the current size. For example, for example:
ArrayList<String> arr = new ArrayList<String>(); arr.add(3,"hi");
Now I already know that there will be an OutOfBoundsException. Is there another way or another object where I can do this while maintaining order? This is because I have methods that detect elements based on their index. For example, for example:
ArrayList<String> arr = new ArrayList<String>(); arr.add("hi"); arr.add(0,"hello");
I would expect to find "hi" in index 1 instead of index 0.
So, in short, if you do not manually insert zero into the intermediate elements, is there a way to satisfy these two requirements:
- Paste the items in a position larger than the current size.
- Click existing items on the right when I insert items in the middle of the list
I reviewed the Java ArrayList by adding an element outside the current size as well as the HashMap, but the HashMap does not meet my second criteria. Any help would be greatly appreciated.
PS Performance is not a problem right now.
UPDATE. There were some questions about why I have these special requirements because I am working on an operational transformation where I insert a set of operations in, say, in my list (mathematical formula). Each operation contains a string. When I insert / delete lines into my list, I will dynamically update unapplied operations (if necessary) by tracking every operation that has already been applied. My real solution now is to use a subclass of ArrayList and override some of the methods. Of course, I would like to know if there is a more elegant way to do this, though.
source share