Java 實例 - List 元素替換

以下實例演示瞭如何使用 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]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章