Question: 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](https://stackoverflow.com/questions/8925128/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.

Answer: 
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 ...)
First and foremost, I would say use `Map` instead of List. I guess your problem can be solved in better way if you use `Map`. But in any case if you really want to do this with `Arraylist`

```
        ArrayList<String> a = new ArrayList<String>(); //Create empty list
        a.addAll(Arrays.asList( new String[100]));  // add n number of strings, actually null . here n is 100, but you will have to decide the ideal value of this, depending upon your requirement.
        a.add(7,"hello");
        a.add(2,"hi");
        a.add(1,"hi2");

```