Adding items to an ArrayList at a position that exceeds the current size

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.

+4
source share
4 answers

Your requirements are contradictory:

... I will need to insert new elements in certain positions.

I need to enter elements at a position larger than the current size.

This means that the positions are stable; that is, an element in a given position remains in that position.

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

This means that in some cases the positions are unstable.

You really need to decide which alternative you need.

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

  • If you do not need to have a stable position, use an ArrayList and consider the case when you need to insert at the end position using append .

I donโ€™t see how reasonable it is for positions to be stable if you insert beyond the end and allow instability if you insert in the middle. (In addition, the latter will ultimately make rapid instability ...)

+6
source

even you can use TreeMap to maintain key order.

+3
source

First of all, I would say instead of using Map . I think your problem can be solved better if you use Map . But anyway, 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"); 
+2
source

Use the Vector class to solve this problem.

Vector vector = new vector (); vector.setSize (100);

vector.set (98, "a");

When setSize is set to 100, all 100 items are initialized to zero.

0
source

Source: https://habr.com/ru/post/1412903/


All Articles