替换ArrayList中的指定元素

import java.util.ArrayList;
import java.util.Collections;

public class ReplaceAllOccurrences {

public static void main(String[] args) {
    // TODO Auto-generated method stub
     ArrayList arrayList = new ArrayList();
        arrayList.add("A");
        arrayList.add("B");
        arrayList.add("A");
        arrayList.add("C");
        arrayList.add("D");
     
        System.out.println("ArrayList Contains : " + arrayList);
       
        Collections.replaceAll(arrayList, "A","Replace All");
       
        System.out.println("After Replace All, ArrayList Contains : " + arrayList);
}

}
Console:
ArrayList Contains : [A, B, A, C, D]
After Replace All, ArrayList Contains : [Replace All, B, Replace All, C, D]

你可能感兴趣的:(替换ArrayList中的指定元素)