Vector implements the List interface and is used to create dynamic arrays. An array that is not fixed in size and can grow as needed is called a dynamic array. Vector is very similar to ArrayList in usage and functionality.
In this article, we will learn how to create a vector in Java and search for a specific element by index. Let's discuss Vector first.
Although Vector is similar to ArrayList in many ways, there are some differences. The Vector class is synchronized, and it contains several legacy methods.
Synchronization - Whenever we perform an operation on a vector, it limits its access to multiple threads at the same time. If we try to access the vector through two or more threads at the same time, it will throw an exception called "ConcurrentModificationException". This makes it less efficient compared to ArrayList.
Old Classes - Before the release of Java 1.2, when the collections framework had not been introduced, there were classes that described the functionality of the framework classes and were used in place of these classes. For example, vectors, dictionaries, and stacks. In JDK 5, Java creators redesigned vectors and made them fully compatible with collections.
We use the following syntax to create vectors.
Vector<TypeOfCollection> nameOfCollection = new Vector<>();
Here, specify the data type of the elements that will be stored in the collection in TypeOfCollection. Give a suitable name for your collection in nameOfCollection.
To search for elements in a Vector by index, we can use this method. There are two ways to use the "indexOf()" method -
indexOf(nameOfObject) - It accepts an object as parameter and returns the integer value of its index. If the object does not belong to the specified collection, only -1 is returned.
indexOf(nameOfObject, index) - It has two parameters, one is the object and the other is the index. It will start searching for the object starting from the specified index value.
In the following example, we will define a vector named "vectlist" and store some objects in it using the "add()" method. Then, using the indexOf() method with a single parameter, we will search for that element.
import java.util.*; public class VectClass { public static void main(String args[]) { // Creating a vector Vector< String > vectList = new Vector<>(); // Adding elements in the vector vectList.add("Tutorix"); vectList.add("Simply"); vectList.add("Easy"); vectList.add("Learning"); vectList.add("Tutorials"); vectList.add("Point"); // storing value of index in variable int indexValue = vectList.indexOf("Tutorials"); System.out.println("Index of the specified element in list: " + indexValue); } }
Index of the specified element in list: 4
The following example demonstrates that "indexOf()" returns -1 if the element is not available in the collection.
import java.util.*; public class VectClass { public static void main(String args[]) { // Creating a vector Vector< String > vectList = new Vector<>(); // Adding elements in the vector vectList.add("Tutorix"); vectList.add("Simply"); vectList.add("Easy"); vectList.add("Learning"); vectList.add("Tutorials"); vectList.add("Point"); // storing value of index in variable int indexValue = vectList.indexOf("Tutorialspoint"); System.out.println("Index of the specified element in list: " + indexValue); } }
Index of the specified element in list: -1
The following example illustrates the usage of "indexOf()" with two parameters. The compiler will search for a given element starting at index 3.
import java.util.*; public class VectClass { public static void main(String args[]) { // Creating a vector Vector< String > vectList = new Vector<>(); // Adding elements in the vector vectList.add("Tutorix"); vectList.add("Simply"); vectList.add("Easy"); vectList.add("Learning"); vectList.add("Tutorials"); vectList.add("Point"); vectList.add("Easy"); vectList.add("Learning"); // storing value of index in variable int indexValue = vectList.indexOf("Easy", 3); System.out.println("Index of the specified element in list: " + indexValue); } }
Index of the specified element in list: 6
In this article, we discussed some examples that demonstrate the usefulness of indexOf() method when searching for a specific element in a Vector. We also learned about Vector in Java.
The above is the detailed content of Search element in vector using index in Java. For more information, please follow other related articles on the PHP Chinese website!