- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Removing Single Elements in a Vector in Java
A single element in the Vector can be removed using the java.util.Vector.remove() method. This method removes the element at the specified index in the Vector. It returns the element that was removed. Also after the element is removed from the specified index, the rest of the Vector elements are shifted to the left.
A program that demonstrates this is given as follows:
Example
import java.util.Vector; public class Demo { public static void main(String args[]) { Vector vec = new Vector(5); vec.add(4); vec.add(1); vec.add(6); vec.add(2); vec.add(8); System.out.println("The Vector elements are: " + vec); vec.remove(2); System.out.println("The Vector elements are: " + vec); } }
The output of the above program is as follows:
The Vector elements are: [4, 1, 6, 2, 8] The Vector elements are: [4, 1, 2, 8]
Now let us understand the above program.
The Vector is created. Then Vector.add() is used to add the elements to the Vector. After that, Vector.remove() method is used to remove the element at index 2 in the Vector. Then the Vector is displayed. A code snippet which demonstrates this is as follows:
Vector vec = new Vector(5); vec.add(4); vec.add(1); vec.add(6); vec.add(2); vec.add(8); System.out.println("The Vector elements are: " + vec); vec.remove(2); System.out.println("The Vector elements are: " + vec);
- Related Articles
- Add elements to a Vector in Java
- Enumerate through the Vector elements in Java
- Add elements at the middle of a Vector in Java
- Loop through the Vector elements using a ListIterator in Java
- Add elements at the end of a Vector in Java
- Append all elements of another Collection to a Vector in Java
- Replace all the elements of a Vector with Collections.fill() in Java
- Clear out all of the Vector elements in Java
- Get first and last elements from Vector in Java
- Removing empty array elements in PHP
- Loop through the Vector elements using an Iterator in Java
- How to Find Single Digit Array Elements in Java?
- How to replace one vector elements with another vector elements in R?
- Adding and Removing Elements in Perl Array
- Removing duplicate elements from an array in PHP

Advertisements