Move the first item to the end

Is there any smart way to do this? my best way:

object next = list.get(0) ; list.remove(0) ; list.add(next) ; 

If not, is there any type of collection that will make this easier? I don't like the need for a temporary object to store the item I want to move.

EDIT: I tested the suggestions listed below with my code:

  long starttime = System.nanoTime() ; for (int i = 0; i < ntours; i++){ profit += retrieveGroupsWillPlay(groups, ngroups, limit) ; } long endtime = System.nanoTime() ; System.out.println("Timing: " + (endtime - starttime)) ; System.out.println("Profit: " + profit) ; 

Here are the results: (profit: 15, make sure the result is correct for my code) code:

 private static int retrieveGroupsWillPlay(ArrayList<Integer> queue,int ngroups, int limit) { int peopleWillPlay = 0 ; for (int i = 0; i < ngroups; i++){ int nextGroup = queue.get(0) ; if(limit >= peopleWillPlay + nextGroup) { peopleWillPlay += nextGroup ; queue.add(nextGroup) ; queue.remove(0) ; } else break ; } return peopleWillPlay ; } 

results:

 Timing: 23326 Profit: 15 Timing: 22171 Profit: 15 Timing: 22156 Profit: 15 Timing: 22944 Profit: 15 Timing: 22240 Profit: 15 Timing: 21769 Profit: 15 Timing: 21866 Profit: 15 Timing: 22341 Profit: 15 Timing: 24049 Profit: 15 Timing: 22420 Profit: 15 

code:

 private static int retrieveGroupsWillPlay(ArrayList<Integer> queue,int ngroups, int limit) { int peopleWillPlay = 0 ; for (int i = 0; i < ngroups; i++){ int nextGroup = queue.get(0) ; if(limit >= peopleWillPlay + nextGroup) { peopleWillPlay += nextGroup ; Collections.rotate(queue, -1) ; } else break ; } return peopleWillPlay ; } 

results:

 Timing: 92101 Profit: 15 Timing: 87137 Profit: 15 Timing: 84531 Profit: 15 Timing: 105919 Profit: 15 Timing: 77019 Profit: 15 Timing: 84805 Profit: 15 Timing: 93393 Profit: 15 Timing: 77079 Profit: 15 Timing: 84315 Profit: 15 Timing: 107002 Profit: 15 

code:

 private static int retrieveGroupsWillPlay(ArrayList<Integer> queue,int ngroups, int limit) { int peopleWillPlay = 0 ; for (int i = 0; i < ngroups; i++){ int nextGroup = queue.get(0) ; if(limit >= peopleWillPlay + nextGroup) { peopleWillPlay += nextGroup ; queue.add(queue.remove(0)) ; } else break ; } return peopleWillPlay ; } 

results:

 Timing: 28079 Profit: 15 Timing: 28994 Profit: 15 Timing: 29525 Profit: 15 Timing: 22240 Profit: 15 Timing: 38326 Profit: 15 Timing: 33742 Profit: 15 Timing: 21500 Profit: 15 Timing: 22714 Profit: 15 Timing: 20939 Profit: 15 Timing: 30157 Profit: 15 

code:

 private static int retrieveGroupsWillPlay(LinkedList<Integer> queue,int ngroups, int limit) { int peopleWillPlay = 0 ; for (int i = 0; i < ngroups; i++){ int nextGroup = queue.get(0) ; if(limit >= peopleWillPlay + nextGroup) { peopleWillPlay += nextGroup ; queue.addLast(queue.removeFirst()) ; } else break ; } return peopleWillPlay ; } 

result:

 Timing: 31104 Profit: 15 Timing: 42332 Profit: 15 Timing: 36443 Profit: 15 Timing: 31840 Profit: 15 Timing: 31387 Profit: 15 Timing: 32102 Profit: 15 Timing: 31347 Profit: 15 Timing: 30666 Profit: 15 Timing: 32781 Profit: 15 Timing: 32464 Profit: 15 

code:

 private static int retrieveGroupsWillPlay(LinkedList<Integer> queue,int ngroups, int limit) { int peopleWillPlay = 0 ; for (int i = 0; i < ngroups; i++){ int nextGroup = queue.get(0) ; if(limit >= peopleWillPlay + nextGroup) { peopleWillPlay += nextGroup ; queue.offer(queue.poll()) ; } else break ; } return peopleWillPlay ; } 

results:

 Timing: 35389 Profit: 15 Timing: 34849 Profit: 15 Timing: 43606 Profit: 15 Timing: 41796 Profit: 15 Timing: 51122 Profit: 15 Timing: 59302 Profit: 15 Timing: 32340 Profit: 15 Timing: 35654 Profit: 15 Timing: 34586 Profit: 15 Timing: 35479 Profit: 15 
+6
source share
6 answers

I'm not quite sure what you want to do, but here goes:

If you use something like ArrayList , you can do:

 list.add(list.remove(0)); 

Please keep in mind that remove from ArrayList works in linear time, i.e. O(N) , therefore it is extremely inefficient.

If you can choose the type of list, you probably want a LinkedList that implements the Dequeue interface, so it will allow you to do something like:

 list.offer(list.poll()); 

Both offer and poll operations are performed at a constant time.

If you want to use the Collections built-in class, you can do it like @dasblinkenlight and use Collections.rotate(list, -1); (adding it here for completeness).

+5
source

You can use Collections.rotate for this:

 Collections.rotate(list, -1); 
+14
source

You do not need the temp variable, just write:

 list.add(list.remove(0)); 
+4
source

The obvious answer is:

list.add(list.remove(0))

which is the most elegant way, I believe. You can also use

Collections.swap(list.get(0), list.get(list.size()-1))

however, this will change the position of another element (the last). You can also use Collections.rotate(list, -1) , however rotating the list can mean moving all of its elements (it depends on the implementation of the List, I think), and it can be inefficient.

+1
source

You want dequeu (short for double terminated queue).

0
source

You can also use the LinkedList # addLast () method.

 list.add(next) ; list.addLast(list.removeFirst()); 
0
source

All Articles