以下实例演示了如何使用 Collections 类的 replaceAll() 来替换List中所有的指定元素:
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", "hundrea");
System.out.println("replaceAll: " + list);
}
}
以上代码运行输出结果为:
List :[one, Two, three, Four, five, six, one, three, Four] replaceAll: [hundrea, Two, three, Four, five, six, hundrea, three, Four]

本文提供了一个使用Java Collections.replaceAll()方法替换List中所有指定元素的示例。演示了如何将List中的某些字符串替换成新的字符串。
4209

被折叠的 条评论
为什么被折叠?



