以下实例演示了如何使用 collections 类的 replaceall() 来替换list中所有的指定元素:
/* author by w3cschool.cc 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", "hundread"); System.out.println("replaceAll: " + list); } }
以上代码运行输出结果为:
List :[one, Two, three, Four, five, six, one, three, Four] replaceAll: [hundread, Two, three, Four, five, six, hundread, three, Four]
以上就是Java 实例 - List 元素替换的内容,更多相关内容请关注PHP中文网(www.php.cn)!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号