使用 Collections中的replaceAll方法 替换list中的指定元素

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

public class ImoocStudent {

    public static void main(String[] args) throws Exception{
        List list = Arrays.asList("one two three four fix six one three".split(" "));
        System.out.println("list:" + list);
        Collections.replaceAll(list,"one","fuck");
        System.out.println("replaceAll:" + list);
    }

}
list:[one, two, three, four, fix, six, one, three]
replaceAll:[fuck, two, three, four, fix, six, fuck, three]
posted @ 2023-05-30 10:36  盘思动  阅读(88)  评论(0编辑  收藏  举报