替换ArrayList里的所有元素

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

public class ReplaceAllElements {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    ArrayList arrayList = new ArrayList();
        arrayList.add("A");
        arrayList.add("B");
        arrayList.add("D");
     
        System.out.println("Before replacement, ArrayList contains : " + arrayList);
       
        Collections.fill(arrayList,"REPLACED");
       
        System.out.println("After replacement, ArrayList contains : " + arrayList);
}

}

Console:
Before replacement, ArrayList contains : [A, B, D]
After replacement, ArrayList contains : [REPLACED, REPLACED, REPLACED]

你可能感兴趣的:(替换ArrayList里的所有元素)