Java 實(shí)例 - List 循環(huán)移動(dòng)元素
以下實(shí)例演示了如何使用 Collections 類(lèi)的 rotate() 來(lái)循環(huán)移動(dòng)元素,方法第二個(gè)參數(shù)指定了移動(dòng)的起始位置:
/* 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); } }
以上代碼運(yùn)行輸出結(jié)果為:
List :[one, Two, three, Four, five, six] rotate: [Four, five, six, one, Two, three]
更多建議: