Java 實例– List 循環移動元素
以下實例演示瞭如何使用Collections 類的rotate() 來循環移動元素,方法第二個參數指定了移動的起始位置:
/* author by w3cschool.cc Main.java */ import java.util.*; public class Main { public static void main(String[] args) { List list = Arrays.asList("one Two three Four five six".split(" ")); System.out.println("List :"+list); Collections.rotate(list, 3); System.out.println("rotate: " + list); } }
以上代碼運行輸出結果為:
List :[one, Two, three, Four, five, six] rotate: [Four, five, six, one, Two, three]