In the previous article, we have discussed about Java Vector elementAt() Method with Example
In this article, we are going to see the use of Java Vector get( ) method along with example.
Java Vector get() Method with Example
This java.util.Vector.get()
method can be used to fetch the element at any specified index just by passing the index to the get( ) method. It returns the index of the element.
Syntax:
vectorName get( int index_of_element))
Where,
vectorName
refers to the name of the Vector.int index_of_element
refers to the location from which you want to fetch the element.
Approach:
- Create an integer vector.
- Add elements to the vector.
- Print the elements of the vector.
- Print the element present at a particular index using get( ) method.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Create a Vector of integer datatype Vector<Integer> vec = new Vector<Integer>(); // Adding some elements to the vector vec.add(100); vec.add(200); vec.add(300); vec.add(400); vec.add(500); // Prints the vector elements System.out.println("The vector elements are "+vec); // Prints the fourth element using get method System.out.println("The fourth element is "+vec.get(3)); } }
Output: The vector elements are [100, 200, 300, 400, 500] The fourth element is 400
Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.
Related Java Programs: