Java 实例 - List 元素替换

以下实例演示了如何使用 Collections 类的 replaceAll() 来替换List中所有的指定元素:

Main.java 文件

  1. import java.util.*;
  2. public class Main {
  3. public static void main(String[] args) {
  4. List list = Arrays.asList("one Two three Four five six one three Four".split(" "));
  5. System.out.println("List :"+list);
  6. Collections.replaceAll(list, "one", "hundrea");
  7. System.out.println("replaceAll: " + list);
  8. }
  9. }

以上代码运行输出结果为:

  1. List :[one, Two, three, Four, five, six, one, three, Four]
  2. replaceAll: [hundrea, Two, three, Four, five, six, hundrea, three, Four]