2

I tried to find an appropriate data structure for my data array. I prefer the one that supports insertion operation (insert in the middle of the list).

If there is no such Java built-in data structure, what is a good way to implement?

E.g. If I use ArrayList<String>, which seems not support insertion in the middle of the list, how to implement the insert(int pos, String str) in a good way?

3 Answers 3

5

Check out the ArrayList.add method (which is declared in the List interface that the ArrayList implements)

Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Sign up to request clarification or add additional context in comments.

6 Comments

Note: add is NOT specific to ArrayList. Every class that implements List supports this operation.
@jahroy: Yes I added that. Also the problem actually is that the docs also mentions that it's an optional operation.
@BheshGurung - I don't understand how a method in an interface can be optional (just re-read the documentation). I'm tempted to ask that in another question!
@jahroy: Typically this should be interpreted as "this method may optionally always throw an UnsupportedOperationException". The language requires you to implement it, but gives you the option of an always-fail implementation that should never be called. (In the case of List, this is explicitly documented: "throws: UnsupportedOperationException - if the add operation is not supported by this list")
The method will always be there, but in some cases its implementation will throw a standard exception at runtime. In this case, it is to support things such as List wrappers for arrays, that do not (cannot) insert extra elements into the wrapped array.
|
2

Any class that implements List can do what you want.

Take a look at List.add(), which inserts an object at a given index.

If you want to replace the existing item, check out List.set().

The add() method is part of the List interface...

However, as the comments under Bhesh Gurung's answer suggest, not all Lists support it.

Therefore, I would go with Bhesh Gurung's answer (as you already have).

5 Comments

What does Set have to do with List?
the method set not the data structure set. duh
Oh... Well, set and add are two different operations. One replaces the existing element, the other inserts before.
Set() replaces an object at the specified index. I believe the OP wants to insert (add at a specified location and shift all other indices to the right). Even worse, if the specified index does not exist, set() throws an IndexOutOfBoundsException
@Mike - Ok, I changed switched the order in which I recommended the two methods: add is now recommended before set.
0

I think you should take a look at LinkedList add(int, E) method which provide better performance for insertion in the middle of the list compared to ArrayList.

4 Comments

... and worse performance for all other operations, especially removing from the middle.
Don't use LinkedList naively. Refer here for when to use it. Both data structure have their strength and weaknesses.
Inserting elements at a given index is possible for every implementation of the List interface. As long as you don't know the OP's requirements, recommending one implementation over another for performance reasons seems doubtful.
@Richard I'm recommending it because the question was asking regarding insertion in the middle of the list. But anyway, I do see your point. Thanks for your comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.