Java Vector Example

Java Vector Example

Java Vector Example: About class Vector, its constructors and methods are explained very elaborately in the first program (and is advised to read before this program) Vector Methods.

Four Java Vector Examples given to know thoroughly about Vector and above methods. Vector is a general purpose data structure user very often by the programmer even though the collections come with many classes.

  1. Vector – Methods: Uses all the methods of Vector class like capacity, size, firstElement etc.
  2. Vector – Generics: Create generics vector storing only integers and uses all Collections static methods like swap, shuffle, fill, rotate etc.
  3. Vector – Retrieval: Illustrates some more methods like get, remove, removeAll and also how to retrieve the elements – 6 styles
  4. Vector – Play With: Give manipulations on vector elements like removing duplicate values, converting vector elements into an array etc.
This is the second Java Vector Example


Java Vector Examplen
Output screen of of Java Vector Retrieval

Six styles of printing elements of a vector are given.

1. Printing direct vector object.
2. Using general for loop with size() method.
3. Using Enumeration.
4. Using Iterator
5. Using ListIterator
6. Using foreach

Vector is mostly used in a multithreaded environment to avoid data inconsistency. All the methods of Vector are implicitly synchronized. So, only one object can do the operation at a time. Vector methods are safe with threads; known as thread safe.

2 thoughts on “Java Vector Example

Leave a Reply

Your email address will not be published. Required fields are marked *