Java Vector remove() MethodThe remove() Java Vector class method is used to remove the specified element from the vector. There are two different types of Java remove() method which can be differentiated depending on its parameter. These are:
remove(int index) Method:This method remove the element at the specified position in this vector. remove(Object o) Method:This method remove the first occurrence of the specified element in this vector. If the vector does not contain the element, it is unchanged. SyntaxFollowing is the declaration of remove() method: Parameter
ReturnThe remove(int index) method returns the element that was removed. The remove(Object obj) method returns true if the vector contained the specified element. ExceptionsArrayIndexOutOfBoundsException- This method has thrown an exception if the index is out of range i.e. (index < 0 || index >= size()). Compatibility VersionJava 1.2 and above Example 1Test it NowOutput: Values in vector: [100, 200, 300, 200, 400] Remove first occourence of element 200: true Values in vector: [100, 300, 200, 400] Example 2Test it NowOutput: Elements of the Vector before remove: [Java, Android, JavaScript, Java] Is the removal successful? true Elements of the Vector after remove: [Android, JavaScript, Java] Example 3Test it NowOutput: Values in vector :[3, 5, 3, 4] Removed element at index 2 is: 3 Values in vector after remove: [3, 5, 4] Example 4Test it NowOutput: Values in vector :[Facebook, Whatsapp, Twitter, Instagram, Skype] Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2 at java.base/java.util.Vector.elementData(Vector.java:761) at java.base/java.util.Vector.remove(Vector.java:875) at myPackage.VectorRemoveExample4.main(VectorRemoveExample4.java:16)
Next TopicJava Vector
|