Vector in Java | Java Vector Class with Examples

Vector in Java | Java Vector Class with Examples

In this tutorial, we'll learn Vector in Java | Java Vector Class with Examples.

In programming, one of the most commonly used data structures is Vector in Java. Arrays are static data structures that can linearly store data. Similarly, vector in java also store the data linearly, but they are not restricted to a fixed size. Instead, its size can grow or shrink as per requirement. The parent class is AbstractList class and is implemented on List Interface.

Before you start to use vectors, import it from the java.util.package as follow:

 import java.util.Vector 

Declaration and Assessing Elements of a Vector

Here is how a vector in java is declared:

public class Vector extends AbstractList

implements List, RandomAccess, Cloneable, Serializable

Here, V is the type of element which can be int, string, char, etc.

Like we access data members in arrays, we can do that in vectors, too, by using the element’s index. For example, the second element of Vector E can be accessed as E[2].

Some common errors made while declaring a vector in java:

  • An IllegalArgumentException is thrown if the initial size of the vector is a negative value
  • A NullPointerException is thrown if the specified collection is null
  • The size of the vector is less than or equal to the capacity of the vector
  • Capacity is doubled in every increment cycle if vector increment is not specified

full stack development java vector vector in java

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Hire Full Stack Developers

If you are looking for a full-stack mobile developer for your web or mobile app development needs? **[Hire Full Stack Developers]( "Hire Full Stack Developers")** to develop any type of web...

Advantages of hiring a full stack developer

Interested in pursuing a career in Full Stack? Join Full Stack Developer Course in Chennai to become an expert in front and back-end technologies. Talk to our counselor for further queries.

Hire Full Stack Developer India

Do you want to get a web application that can meet your business requirements successfully? We love to work with startups and enterprises to solve their business problems using our full-stack technology competencies. Our expertise in agile and...

Java Development Company in USA | Java Application Development Company

We offer java app development & web app services at affordable rates. Let our Java Developers customize for you to provide high performance & reliable Java based solutions.

Hire Dedicated Java Developer and Offshore Java Developers

For world-class java web application development services you must hire dedicated java developer from a reputed software development company – ByteCipher Pvt. Ltd