Java Vector

Ads
 
Tutorials   
Java/JDK 15 upcoming new features JDK/Java 15 Tutorials How to install OpenJDK 8 in Ubuntu 18.04? Java Interpreter is used for which purpose? Java program to read a text file and write to another file How to Serialize and Deserialize Array List in Java? What are the prerequisites for learning Java? How to replace all occurrences of a string in JavaScript? How to convert a stack trace to a string? Example code of reading file line by line in Java with Apache Commons IO library How to store unique values in Java using Set? java.nio ShortBuffer Example How to use Java Path class? Java NIO Package Tutorial How to limit object creation in Java? Java 11 String lines function example to split it in lines Top upcoming features of JDK 11 Java/JDK 11 Tutorial How to install JDK 10 in Ubuntu 18.04? Install JDK 10 on Windows Java 10 features Processing large file in Java Learning Java for non programmers Why You Should Learn Java in 2018? Installing JDK 9 on Windows 10 How to convert binary to decimal in Java? How to add two numbers in Java? What is Applet in Java with Example? What is nested class in Java with Example? Exception handling in Java Programming Examples What is Constructor Overloading in Java? How to display image in Swing? What is Java programming used for? How to create custom exception in Java? How to set CLASSPATH in Java? What is Multithreading in Java? IO Streams in Java with Examples How to convert String Date to Timestamp in Java? Best Java Tutorial for Beginners Read user input in Java using Scanner

Java Vector

Vectors are used instead of arrays because unlike array they can adjust according to the component added or removed from them.

Vectors are used instead of arrays because unlike array they can adjust according to the component added or removed from them.

Vectors in Java are used because they can expand whenever a new data is added to them. They also shrink when data is removed from them. The Vector class implements dynamic array of objects.

Vectors can only hold objects. Components are added to Vector during runtime.

Items can be removed even after the Vector has been created.

Vectors are array list with extended properties which follow the dynamic and automatic addition of data at run-time. The Component in vector class can be accessed by integer index.

Capacity of vector is usually larger as components are added in it during run time.

Size of vectors increases in chunks.

Vector instances are like linked-lists, whenever they reach maximum limit, resources are allocated dynamically and its size is increased further.

To create a Vector import java.util.Vector or java.util.*

How to declare Vector

Following Syntax is used to declare an empty vector so that its standard storage capacity is zero

public Vector()

Following Syntax constructs an empty vector specified by initial capacity:

vector(int initial capacity)

Following Syntax creates an empty vector with the specified initial capacity and capacity increment:

vector(int initial capacity,int capacity increment)

Method used in Vector

Following method inserts the specified element at the specified position in the Vector:

void add(int index, Object element)

Following method appends the specified element to the end of the Vector:

boolean add(object o)

Following method return the current capacity of the vector:

int capacity( )

Following method removes all of the elements from the Vector:

void clear( )

Following method sets the new size of Vector

void setsize(int new size )

Following method returns the number of Component in Vector:

int size( )

Following method trims the Capacity of Vector to vector's current size:

Vector trim to size( )

Following method returns the occurrence of last index of the specified object in the vector:

public int lastindexof(obj abc)

Following is the Example of Java Vector:

import java.util.Vector;
import java.util.Collections;

public class maximum vector element
 {

  public static void main(String[] args)

 {
 
  //create a Vector object
  Vector v = new Vector();
 
  //Add elements to Vector
  v.add(new int("500"));
  v.add(new int("745"));
  v.add(new int("842"));
  v.add(new int("900"));
  v.add(new int("567"));
 
  
 
  Object obm = Collections.max(v);
 
  System.out.println("Maximum Element of Java Vector is : " + obm);
  }
}

Output:

Maximum Element of java Vector is :900

Ads

Ads