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 40 41 42 43 44 45 |
/* Replace all occurrences of specified element of Java Vector Example This java example shows how to replace all occurrences of a specified element of Java Vector using replaceAll method of Collections class. */ import java.util.Vector; import java.util.Collections; public class ReplaceAllVectorExample { 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); } } /* Output would be Vector Contains : [A, B, A, C, D] After Replace All, Vector Contains : [Replace All, B, Replace All, C, D] */ |
Add Comment