I have an array list with a size of 10 elements. What I am trying to achieve here is that when I add an element at any index, the element which is next to it should get shifted except at some fixed indexes.
For example, Let's say my array list is as follows
[10, 20, 30, 40, 50,60,70,80,90,100]
indexes are {0,1,2,3,4,5,6,7,8,9}
fixed indexes{4,6}
when I remove an element from index 10 and add an index 3 the output should be as follows
[10, 20, 30, 100, 50, 40,70,60,80,90,]
if you notice the values at indexes {4,6} did not change after adding 10 th element at index 3.
List<Integer> values = new ArrayList<>();
values.add(10);
values.add(20);
values.add(30);
values.add(40);
values.add(50);
values.add(60);
values.add(70);
values.add(80);
values.add(90);
values.add(100);
System.out.println(values);
values.removeAt(9)
values.add(3,100);
System.out.println(values);
# Output
[10, 20, 30, 40, 50,60,70,80,90,100]
[10, 20, 30, 100, 40,50,60,70,80,90]
If anyone can Suggest any sorting method or collections to achieve this it will be really helpful.
Sample input and outputs:
int[] a= [7, 4, 3, 2, 1, 5, 6, 4, 8, 9] and fixed indexes are {1,2}
Remove 6th index element (6) and add at index 0 The output should be as follows:
[6, 4, 3, 7, 2, 1, 5, 4, 8, 9]
{0,1,2,3,4,5,6,7,8,9,10}that is 11 elements.