本文整理匯總了Java中java.util.Collections.replaceAll方法的典型用法代碼示例。如果您正苦於以下問題:Java Collections.replaceAll方法的具體用法?Java Collections.replaceAll怎麽用?Java Collections.replaceAll使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.Collections
的用法示例。
在下文中一共展示了Collections.replaceAll方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: main
import java.util.Collections; //導入方法依賴的package包/類
public static void main(String[] args) {
//create a Vector object
Vector v = new Vector();
//Add elements to Vector
v.add("A");
v.add("B");
v.add("A");
v.add("C");
v.add("D");
System.out.println("Vector Contains : " + v);
/*
To replace all occurrences of specified element of Java Vector use,
static boolean replaceAll(List list, Object oldVal, Object newVal) method
of Collections class.
This method returns true if the list contained one more elements replaced.
*/
Collections.replaceAll(v, "A", "Replace All");
System.out.println("After Replace All, Vector Contains : " + v);
}
示例2: main
import java.util.Collections; //導入方法依賴的package包/類
public static void main(String[] args) {
//create an ArrayList object
ArrayList arrayList = new ArrayList();
//Add elements to Arraylist
arrayList.add("A");
arrayList.add("B");
arrayList.add("A");
arrayList.add("C");
arrayList.add("D");
System.out.println("ArrayList Contains : " + arrayList);
/*
To replace all occurrences of specified element of Java ArrayList use,
static boolean replaceAll(List list, Object oldVal, Object newVal) method
of Collections class.
This method returns true if the list contained one more elements replaced.
*/
Collections.replaceAll(arrayList, "A", "Replace All");
System.out.println("After Replace All, ArrayList Contains : " + arrayList);
}