How to Use Vector in Java to Create Variable Sized Arrays
Every programming language makes provision for saving a number of elements in a structure. Most programming languages include array functions for this. Arrays allow a programmer to store various programming elements within a fixed sized data storage device. Generally elements within the array can be accessed via a specific indexing system. Java goes a step further than an array by providing other specific data structures that allow the programmer to save and manipulate various elements in data structures that do not require a fixed size. The vector data structure in java is an example of one of these java data structures.
This tutorial is designed to show you how the vector data structure can be implemented in java. The tutorial assumes that you have some basic java coding experience. It also assumes that that you understand some basic java principles. To learn how to implement java and the other basic principles required by the tutorial, you can consider a course like programming Java for Beginners – The Ultimate Java Tutorial.
The vector data structure is best for saving collection where the size of the initial collection is unknown or where the size of the collection may vary from time to time. For our example we will use a movie collection. Let’s say an avid movie fanatic has asked you to create an application to keep track of his movie collection. He isn’t sure how many movies he has and he also buys, sells and swaps his movies with other collectors. His collection may therefore grow or shrink at various times so using a fixed array structure may not work in this situation. As mentioned above, the best way to achieve this is to use the vector structure in java..
Creating the initial Vector structure.
The first thing we need to do to create the application, is to import the library that contains the function. If you don’t know how object oriented programs work or why you need to import libraries then a Java for Absolute Beginners will teach you how to import libraries as well as how to create, name and save your java applications.
There are four ways to initialize the vector data structure:
1. Initializing the vector data structure without any parameters creates a new vector structure with an initial capacity of ten:
2. You can initialize a vector and specify the initial capacity of the vector:
3. You can also specify the increment of the vector. This means you can specify how many elements are added to the vector each time it increases in size.
4. You can initialize a vector which contains the elements of a collection c:
So for our example, we will initiate our vector using the first structure since we do not know the initial size of the movie collection our collector has..
Now that we have created a new vector that contains the names of our movie collection, the initial size of which is unknown, we can use the methods available to the java vector structure to add movies to our program.
Adding, deleting and finding movies within our vector
The vector data structure has certain predefined methods which allow you to add, modify, delete and manipulate the elements within the structure. There are over forty two methods which can be applied to the data structure but for the purposes of this tutorial we will focus on adding an element, finding an element and removing an element. For more advanced java programming training, you could take a course at udemy.
To add an element to a vector you need to use the .add() function available to the vector. When you add an element to the vector data structure, the structure’s capacity is increased incrementally either by one for each element added, or by the value specified when you declared the vector.
The syntax to add an element to a vector is expressed as follows:
In our example, each time we add an element the capacity of our structure is automatically increased by one. So by adding a movie to our structure, the size of our structure would be one. Here is an example of the code you would need to add one movie to our movie collection:
To find the element within the vector you can use the get function of the vector data structure. The get element fetches the value of an element at a particular index value. Indexes begin counting at position 0. If you don’t know how to work with indexes or how indexes work then you can learn Java from scratch at udemy. To use the get function, you need to use the following syntax:
To fetch a value within our vector and display that value to the screen, the code would look like this:
To remove an element from our vector, we use the remove function. When you remove an element from the structure, the size of the structure is automatically adjusted accordingly. This is what makes the vector data structure a good structure to work with if the size of the array is unknown or if the size of the array is constantly changing. The syntax used to remove an element from a vector data structure is:
For our example, let us add three movies to our database and then remove the middle one. The middle movie will be index position 1. The code to achieve this would thus be:
Finally to find out how many movies are in our vector, we would use the vector size function which returns the number of elements within the vector. So if we wanted to know how many movies were now part of our collection we would use the following syntax:
So let us add all of the functions above to create a vector which allows us to add movies to our collection, remove a movie, display the movies in our collection and finally tell us how many movies there are in our collection. The final code would like this:
The output of the above code would look like this:
Vectors are a powerful way of manipulating arrays where the size of the array is unknown. For more advanced java programming tutorials, you can take the Java Fundamentals I and II course at udemy which will have your programming data structures in no time at all.
Top courses in Java
Java students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.