Add an element in between two other elements in an ArrayList

Hi, fairly new person to Java here, I’d like to ask if there’s a way to add an element between two elements or be able to shift all elements to the right or left of an ArrayList and how I could figure out the middle.

For example if I have [3, 5, 8, 10] and I want to insert the number 6, how would I figure out where it fits in ascending order?

Thanks so much

Well, one way is to just binary search to find the proper index, and then just do

arr.add(index,val);

If you want to keep a sorted list without the hassle of knowing where to put future elements, try using a PriorityQueue (which allows duplicates) or a TreeSet. (doesn’t allow duplicates) Both are sorted in ascending order automatically.