Javatpoint Logo
Javatpoint Logo

Java Vector remove() Method

The 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:

  1. Java Vector remove(int index) Method
  2. Java Vector remove(Object o) Method

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.

Syntax

Following is the declaration of remove() method:

Parameter

Parameter Description Required/Optional
index It is an index of the element which will be removed. Required
obj It is an element which will be removed from the vector if present. Required

Return

The remove(int index) method returns the element that was removed.

The remove(Object obj) method returns true if the vector contained the specified element.

Exceptions

ArrayIndexOutOfBoundsException- This method has thrown an exception if the index is out of range i.e. (index < 0 || index >= size()).

Compatibility Version

Java 1.2 and above

Example 1

Test it Now

Output:

Values in vector: [100, 200, 300, 200, 400]
Remove first occourence of element 200: true
Values in vector: [100, 300, 200, 400]

Example 2

Test it Now

Output:

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 3

Test it Now

Output:

Values in vector :[3, 5, 3, 4]
Removed element at index 2 is: 3
Values in vector after remove: [3, 5, 4]

Example 4

Test it Now

Output:

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





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA