1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
/* Replace All Elements Of Java Vector Example This java example shows how to replace all elements of Java Vector object with the specified element using fill method of Collections class. */ import java.util.Vector; import java.util.Collections; public class ReplaceAllElementsOfVectorExample { 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("D"); /* To replace all elements of Java Vector with specified element use, static void fill(List list, Object element) method of Collections class. */ System.out.println("Before replacement, Vector contains : " + v); Collections.fill(v,"REPLACED"); System.out.println("After replacement, Vector contains : " + v); } } /* Output would be Before replacement, Vector contains : [A, B, D] After replacement, Vector contains : [REPLACED, REPLACED, REPLACED] */ |
Add Comment