Java 實例 - List 迴圈移動元素
以下實例演示了如何使用 Collections 類的 rotate() 來迴圈移動元素,方法第二個參數指定了移動的起始位置:
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]