将列表的第一个元素移到末尾

将列表的第一个元素移到末尾

本文介绍了将列表的第一个元素移到末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么聪明的方法吗?我最好的方法是:

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

如果没有,有什么类型的收藏可以使这变得容易吗?我不希望需要一个临时对象来存储要移动的元素。



编辑:我已经用我的代码测试了下面列出的命题:

 长启动时间= System.nanoTime(); 
for(int i = 0; i< ntours; i ++){
利润+ = RetrieveGroupsWillPlay(groups,ngroups,limit);
}
长终止时间= System.nanoTime();
System.out.println( Timing: +(endtime-starttime));
System.out.println( Profit: +利润);

结果如下:(利润:15,确保结果适合我的代码)
代码:

 私有静态int resolveGroupsWillPlay(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;
}

结果:

 时间:23326 
利润:15
时间:22171
利润:15
时间:22156
利润:15
时间:22944
利润:15
时间:22240
利润:15
时间:21769
利润:15
时间:21866
利润:15
时间:22341
利润:15
时间:24049
利润:15
时间:22420
利润:15

代码:

 私有静态int resolveGroupsWillPlay(ArrayList< Integer>队列,in​​t 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;
}

结果:



<$时间:92101
利润:15
时间:87137
利润:15
时间:84531
利润:15
时间:105919
利润:15
时间:77019
利润:15
时间:84805
利润:15
时间:93393
利润:15
时间:77079
利润:15
时间:84315
利润:15
时间:107002
利润:15

代码:

 私有静态int resolveGroupsWillPlay(ArrayList< Integer>队列,in​​t 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;
}

结果:

 时间:28079 
利润:15
时间:28994
利润:15
时间:29525
利润:15
时间:22240
利润:15
时间:38326
利润:15
时间:33742
利润:15
时间:21500
利润:15
时间:22714
利润:15
时间:20939
利润:15
时间:30157
利润:15

代码:

 私有静态int restoreGroupsWillPlay(LinkedList< Integer>队列,in​​t 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;
}

结果:

 时间:31104 
利润:15
时间:42332
利润:15
时间:36443
利润:15
时间:31840
利润:15
时间:31387
利润:15
时间:32102
利润:15
时间:31347
利润:15
时间:30666
利润:15
时间:32781
利润:15
时间:32464
利润:15

代码:

 私有静态int restoreGroupsWillPlay(LinkedList< Integer>队列,in​​t 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;
}

结果:

 时间:35389 
利润:15
时间:34849
利润:15
时间:43606
利润:15
时间:41796
利润:15
时间:51122
利润:15
时间:59302
利润:15
时间:32340
利润:15
时间:35654
利润:15
时间:34586
利润:15
时间:35479
利润:15


解决方案

我不确定您要做什么,但是可以:



如果您使用的是 ArrayList 之类的方法,则可以这样做:

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

请记住,从ArrayList是线性运行的,即 O(N),因此效率极低。



您可以选择列表的类型,您可能需要一个 LinkedList ,它实现 Dequeue 接口,因此它将允许您可以执行以下操作:

  list.offer(list.poll()); 

两者均提供 poll 是在恒定时间内完成的操作。



如果要使用 Collections 类,您可以按照@dasblinkenlight的建议进行操作,并使用 Collections.rotate(list,-1); (为完整性起见在此添加)。


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:

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
解决方案

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

If you're using something like an ArrayList, you can do:

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.

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.offer(list.poll());

Both offer and poll are operations done in constant time.

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).

这篇关于将列表的第一个元素移到末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 02:27