本文整理汇总了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);
}