0

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

1
  • 2
    It's the add operation of the List interface. You don't have to shift the elements, the add(...) takes care of that. Commented Aug 3, 2021 at 0:54

1 Answer 1

2

Depends on your need, do you necessary need an ArrayList?

#1) If you need sorting but is the number unique? The provided example is (3, 5, 8, 10) and to add (6). If there is another '3', should the result return (3,3,5,6,8,10)? If NOT, why not consider a NavigableSet/SortedSet such as TreeSet ?

import java.util.*;
...
NavigableSet<Integer> navSet = new TreeSet<>(Arrays.asList(new Integer[]{3,5,8,10});
navSet.add(6); //you'll get 3,5,6,8,10
navSet.add(6); //still get 3,5,6,8,10 -- no extra 6

#2) Is the number non unique but to be ordered ? There is a collection provided by Apache for BAG(combining Set and List). Apache Commmons Collection Bag

import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.bag.TreeBag;
...
Bag<Integer> treeBag = new TreeBag<>(Arrays.asList(new Integer[]{3,5,6,8,10});
treeBag.add(6); //you'll get 3,5,6,6,8,10

#3) Else if you really need to code it manually. There is always a Collections util to sort it after you add, e.g.

import java.util.*;

List<Integer> arrayList = Arrays.asList(new Integer[]{3,5,5,8,10});
List sortedList = arrayList.stream().parallel().sorted()collect(Collectors.toList());
//you'll get (3,5,5,6,8,10), as it gets sorted again.

Finally, the sorting above for #2 and #3 is always slower as there is an add then re-sort. If you want something that optimized via search while rebuilding the list, like Stephen P provided in the other answer, it may be a better solution. But as always consider your implementation.

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

4 Comments

@Basil Borque, thanks. Learned something today about NavigableSet, it's really helpful with the edit.
Good. I just now changed your first code sample to capture a NavigableSet rather than Set. Though not technically necessary, it makes intentions explicit as well as makes available the various methods specific to NavigableSet should they be required in further coding. And I changed var name from treeSet to navSet to match.
By the way, I happened to have just now coincidentally written another Answer using NavigableSet. You might find it interesting.
didn't know sorted sets existed until now thanks lol

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.