java 将列表的第一个元素移到末尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14566118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Move the first element of a list to the end
提问by elaich
is there any clever way to do that ? my best way was:
有什么聪明的方法可以做到这一点吗?我最好的方法是:
object next = list.get(0) ;
list.remove(0) ;
list.add(next) ;
if not is there any type of collection that will make that easier ? I don't like the need of a temporary object to store the element i want to move ..
如果没有,是否有任何类型的收藏可以使这更容易?我不喜欢需要一个临时对象来存储我想要移动的元素..
EDIT: I have tested the propositions 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 is the results: (profit: 15, ensure that the result is right for my code) code:
这是结果:(利润:15,确保结果适合我的代码)代码:
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
回答by dasblinkenlight
You can use Collections.rotate
for that:
你可以使用Collections.rotate
:
Collections.rotate(list, -1);
回答by pcalcao
I'm not exactly sure what you want to do, but here goes:
我不确定你想做什么,但这里有:
If you're using something like an ArrayList
, you can do:
如果您使用的是类似的东西ArrayList
,您可以执行以下操作:
list.add(list.remove(0));
Please keep in mind that remove
from an ArrayList runs in linear time, that is, O(N)
, so that is extremely inefficient.
请记住,remove
从 ArrayList 以线性时间运行,即O(N)
,因此效率极低。
In case you can choose the type of List, you probably want a LinkedList
, that implementes the Dequeue
interface, so it would allow you to do something like:
如果您可以选择 List 的类型,您可能需要一个LinkedList
实现Dequeue
接口的 , 这样您就可以执行以下操作:
list.offer(list.poll());
Both offer
and poll
are operations done in constant time.
双方offer
并poll
在不断的时间内完成操作。
If you want to use a builtin from the Collections
class, you can do as @dasblinkenlight suggested and use Collections.rotate(list, -1);
(adding it here for completeness).
如果您想使用Collections
类中的内置函数,您可以按照@dasblinkenlight 的建议进行操作并使用Collections.rotate(list, -1);
(在此处添加以确保完整性)。
回答by codeghost
You don't need a temp variable just write:
您不需要临时变量,只需编写:
list.add(list.remove(0));
回答by Betalord
The obvious answer is:
显而易见的答案是:
list.add(list.remove(0))
list.add(list.remove(0))
which is the most elegant way, I believe. You could alternatively use
我相信这是最优雅的方式。你也可以使用
Collections.swap(list.get(0), list.get(list.size()-1))
Collections.swap(list.get(0), list.get(list.size()-1))
however that will change position of another element (the last one). You could also use Collections.rotate(list, -1)
, however rotating the list could mean moving all of its elements (it depends on List implementation I guess) and that might not be efficient.
但是这会改变另一个元素(最后一个)的位置。您也可以使用Collections.rotate(list, -1)
,但是旋转列表可能意味着移动其所有元素(我猜这取决于 List 实现)并且这可能效率不高。
回答by PermGenError
You can also use LinkedList#addLast()method.
您还可以使用LinkedList#addLast()方法。
list.add(next) ;
list.addLast(list.removeFirst());