Javaexamples-collection-rotate

提供:Dev Guides
移動先:案内検索

Javaの例-リスト内の要素の回転

問題の説明

リストの要素を回転させる方法は?

溶液

次の例では、メソッドの2番目の引数に応じて、rotate()メソッドを使用してリストの要素を回転させます。

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]