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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
/* Search an element of Java Vector Example This Java Example shows how to search an element of java Vector object using contains, indexOf and lastIndexOf methods. */ import java.util.Vector; public class SearchAnElementInVectorExample { public static void main(String[] args) { //create a Vector object Vector v = new Vector(); //Add elements to Vector v.add("1"); v.add("2"); v.add("3"); v.add("4"); v.add("5"); v.add("1"); v.add("2"); /* To check whether the specified element exists in Java Vector use boolean contains(Object element) method. It returns true if the Vector contains the specified objct, false otherwise. */ boolean blnFound = v.contains("3"); System.out.println("Does Vector contain 3 ? " + blnFound); /* To get an index of specified element in Vector use int indexOf(Object element) method. This method returns the index of the specified element in Vector. It returns -1 if not found. */ int index = v.indexOf("5"); if(index == -1) System.out.println("Vector does not contain 5"); else System.out.println("Vector contains 5 at index :" + index); /* To get last index of specified element in Vector use int lastIndexOf(Object element) method. This method returns index of the last occurrence of the specified element in Vector. It returns -1 if not found. */ int lastIndex = v.lastIndexOf("2"); if(lastIndex == -1) System.out.println("Vector does not contain 2"); else System.out.println("Last occurrence of 2 in Vector is at index :" + lastIndex); } } /* Output would be Does Vector contain 3 ? true Vector contains 5 at index :4 Last occurrence of 2 in Vector is at index :6 */ |
thank you!