以下實例示範如何使用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)!