Java サンプル - List の要素を循環移動

Java 实例Java サンプル

以下のサンプルは、Collections クラスの rotate() を使用して要素を循環移動する方法を示しています。メソッドの2番目のパラメータが移動の開始位置を指定します。

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 实例Java サンプル

その他の拡張