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

ela*_*ich 7 java collections

有没有聪明的方法呢?我最好的方法是:

object next = list.get(0) ;
list.remove(0) ;
list.add(next) ;
Run Code Online (Sandbox Code Playgroud)

如果没有,是否有任何类型的集合,将使这更容易?我不喜欢需要一个临时对象来存储我想要移动的元素..

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

    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) ;
Run Code Online (Sandbox Code Playgroud)

结果如下:(利润: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 ;
}
Run Code Online (Sandbox Code Playgroud)

结果:

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
Run Code Online (Sandbox Code Playgroud)

码:

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 ;
}
Run Code Online (Sandbox Code Playgroud)

结果:

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
Run Code Online (Sandbox Code Playgroud)

码:

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 ;
}
Run Code Online (Sandbox Code Playgroud)

结果:

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
Run Code Online (Sandbox Code Playgroud)

码:

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 ;
}
Run Code Online (Sandbox Code Playgroud)

结果:

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
Run Code Online (Sandbox Code Playgroud)

码:

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 ;
}
Run Code Online (Sandbox Code Playgroud)

结果:

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 
Run Code Online (Sandbox Code Playgroud)

das*_*ght 16

你可以用Collections.rotate它:

Collections.rotate(list, -1);
Run Code Online (Sandbox Code Playgroud)


pca*_*cao 8

我不确定你想做什么,但是这里有:

如果您使用类似的东西ArrayList,您可以:

list.add(list.remove(0));
Run Code Online (Sandbox Code Playgroud)

请记住,remove从ArrayList运行的线性时间,即O(N),这是非常低效的.

如果你可以选择List的类型,你可能想要一个LinkedList实现Dequeue接口的东西,所以它允许你做类似的事情:

list.offer(list.poll());
Run Code Online (Sandbox Code Playgroud)

双方offerpoll在不断的时间内完成操作.

如果你想使用Collections类中的内置函数,你可以像@dasblinkenlight建议和使用那样Collections.rotate(list, -1);(在这里添加它是为了完整性).


cod*_*ost 5

你不需要一个临时变量写:

list.add(list.remove(0));
Run Code Online (Sandbox Code Playgroud)