Moving list items
I have a list of objects and I want to do some kind of operation on the list in such a way that a particular object should be shifted to list position 0, and the object at position 0 will have a shifted object. the diagram is shown below.
the list looks like this
final List<Object> list= new ArrayList<Object>();
Currently I have made two temporary lists as
final List<Object> temp1= new ArrayList<Object>();
final List<Object> temp2= new ArrayList<Object>();
to perform the operation, I start a loop and on a specific condition, adding an object to temp1, adding to temp2, something like the following:
for (int i = 0; i < 5; i++) {
if (i==3) {
temp1.add(i);
} else {
temp2.add(i);
}
}
and finally doing
list.addAll(temp1);
list.addAll(temp2);
how to do the same logic in redundant and efficient steps instead of using temporary lists.
+3
source to share