Java 实例 - List 循环移动元素


以下实例演示了如何使用 Collections 类的 rotate() 来循环移动元素,方法第二个参数指定了移动的起始位置:

  1. /*
  2. author by shouce.ren
  3. Main.java
  4. */
  5.  
  6. import java.util.*;
  7.  
  8. public class Main {
  9. public static void main(String[] args) {
  10. List list = Arrays.asList("one Two three Four five six".split(" "));
  11. System.out.println("List :"+list);
  12. Collections.rotate(list, 3);
  13. System.out.println("rotate: " + list);
  14. }
  15. }

以上代码运行输出结果为:

  1. List :[one, Two, three, Four, five, six]
  2. rotate: [Four, five, six, one, Two, three]