开发者社区> 问答> 正文

Java 实例 - List 元素替换

Java 实例 - List 元素替换

展开
收起
问问小秘 2020-02-13 17:32:48 1048 0
1 条回答
写回答
取消 提交回答
  • 以下实例演示了如何使用 Collections 类的 replaceAll() 来替换List中所有的指定元素:

    Main.java 文件

    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]
    
    2020-02-13 17:33:16
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载