Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding elements into ArrayList at position larger than the current size

Currently I'm using an ArrayList to store a list of elements, whereby I will need to insert new elements at specific positions. There is a need for me to enter elements at a position larger than the current size. For e.g:

ArrayList<String> arr = new ArrayList<String>();
arr.add(3,"hi");

Now I already know there will be an OutOfBoundsException. Is there another way or another object where I can do this while still keeping the order? This is because I have methods that finds elements based on their index. For e.g.:

ArrayList<String> arr = new ArrayList<String>();
arr.add("hi");
arr.add(0,"hello");

I would expect to find "hi" at index 1 instead of index 0 now.
So in summary, short of manually inserting null into the elements in-between, is there any way to satisfy these two requirements:

  • Insert elements into position larger than current size
  • Push existing elements to the right when I insert elements in the middle of the list

I've looked at Java ArrayList add item outside current size, as well as HashMap, but HashMap doesn't satisfy my second criteria. Any help would be greatly appreciated.

P.S. Performance is not really an issue right now.

UPDATE: There have been some questions on why I have these particular requirements, it is because I'm working on operational transformation, where I'm inserting a set of operations into, say, my list (a math formula). Each operation contains a string. As I insert/delete strings into my list, I will dynamically update the unapplied operations (if necessary) through the tracking of each operation that has already been applied. My current solution now is to use a subclass of ArrayList and override some of the methods. I would certainly like to know if there is a more elegant way of doing so though.

like image 490
Wei Hao Avatar asked Dec 21 '22 21:12

Wei Hao


1 Answers

Your requirements are contradictory:

... I will need to insert new elements at specific positions.

There is a need for me to enter elements at a position larger than the current size.

These imply that positions are stable; i.e. that an element at a given position remains at that position.

I would expect to find "hi" at index 1 instead of index 0 now.

This states that positions are not stable under some circumstances.

You really need to make up your mind which alternative you need.

  • If you must have stable positions, use a TreeMap or HashMap. (A TreeMap allows you to iterate the keys in order, but at the cost of more expensive insertion and lookup ... for a large collection.) If necessary, use a "position" key type that allows you to "always" generate a new key that goes between any existing pair of keys.

  • If you don't have to have stable positions, use an ArrayList, and deal with the case where you have to insert beyond the end position using append.

I fail to see how it is sensible for positions to be stable if you insert beyond the end, and allow instability if you insert in the middle. (Besides, the latter is going to make the former unstable eventually ...)

like image 124
Stephen C Avatar answered Mar 02 '23 01:03

Stephen C