Javaexamples-collection-replace

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

Javaの例-リスト内の要素を置き換える

問題の説明

リスト内の要素を置き換える方法

溶液

次の例では、replaceAll()メソッドを使用して、要素のすべての出現をリスト内の別の要素に置き換えます。

import java.util.*;

public class Main {
   public static void main(String[] args) {
      List list = Arrays.asList("one Two three Four five six one three Four".split(" "));
      System.out.println("List :"+list);
      Collections.replaceAll(list, "one", "hundread");
      System.out.println("replaceAll: " + list);
   }
}

結果

上記のコードサンプルは、次の結果を生成します。

List :[one, Two, three, Four, five, six, one, three, Four]
replaceAll: [hundread, Two, three, Four, five, six, hundread, three, Four]