Java Vector Generics Example

Java Vector Generics Example


Java Vector Generics 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 Generics Examples are given on Vector to know thoroughly about Vector, methods and usage. Vector is a general purpose data structure used 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, shuttle, 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.
Java Vector Generics Example: Two Vector objects are created storing only int values and string values.


Java Vector Generics Example
Output screen of VectorGenerics.java of Java Vector Generics

List numbersList = vect2.subList(1, 9);

Three Java Vector Generics objects storing only integers (vect1, vect3) and strings(vect2) are created. subList(1, 9) returns a List object containing vector vect2 elements from 1 to 8 (9-1).

Collections.swap(vect2, 2, 4);

The elements 2 and 4 of vect2 are swapped. That is, in the place of 2 four comes and in 4 place 2 comes. See the screenshot.

System.out.println(“Maximum value of vect1: ” + Collections.max(vect1));
System.out.println(“Minimum value of vect1: ” + Collections.min(vect1));

max(vect1) returns the maximum value element in vect1 and min(vect1) returns the minimum value element in vect1 vector.

vect1.addAll(vect3);

All the elements of vector vect3 are added to vect1. vect3 elements are added at the end (appended) of vect1 elements.

Collections.reverse(vect3);

All the elements of vect3 are reversed. It affects the original vector itself. Now vect3 contains the elements in reverse order.

Collections.shuffle(vect1);

The static method shuffle(vect1) of Collections class shuffles the elements in the vector vect1. Calling the same method a number of times, prints the elements in different orders. After this method call, vect1 contains elements in a different order.

Collections.rotate(vect1, 3);

The static method rotate(vect1, 3) of Collections class rotates the elements. First it prints the last 3 and afterwards 0 to size()-3.

Collections.fill(vect1, 100);

The fill() fills all the elements of vect1 with 100. The earlier values are lost. It helps to have common values in all the elements.

vect1.clear();

The clear() method removes all the elements. Now the size becomes zero.

String str = vect3.toString();

The vector object vect3 is converted to string str. The str contains all the elements of vect3. The string format is helpful when required to display in text field. Learn more methods of Collections class.

Leave a Reply

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