欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > > 文章正文

Java实例-List元素替换

来源: javaer 分享于  点击 40530 次 点评:83

Java实例-List元素替换


Java 实例 Java 实例

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

Java 实例 Java 实例

用户点评