以下實例示範如何使用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]
以上就是Java 實例- List循環移動元素的內容,更多相關內容請關注PHP中文網(www.php.cn)!