使用 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 @   盘思动  阅读(111)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2020-05-30 如何设定用F12进入bios
2019-05-30 获取函数一次执行中回调次数
点击右上角即可分享
微信分享提示