Java 教程 Java 实例 - List 元素替换

skip · 2022-06-11 14:32:28 · 热度: 51

以下实例演示了如何使用 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]

猜你喜欢:
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册