Get Vector elements

There are many methods to retrieve or get element from Vector


Object get(int index);
Object firstElement();
Object elementAt(int index);

Object get(int index)

We can use get(int index) method for retrieving the element from Vector

It returns the value present at the specified index

This method throws IndexOutOfBoundsException if the index is less than zero or greater than the size of the Vector

Example :

  1. import java.util.*;
  2. public class VectorGetElement {
  3.     public static void main(String[] args) {
  4.     Vector<String> vector = new Vector<String>();
  5.     vector.add("java");
  6.         vector.add("c++");
  7.         vector.add("python");
  8.         vector.add("c");
  9.         System.out.println("First element of the Vector: "+vector.get(0));
  10.         System.out.println("Third element of the Vector: "+vector.get(2));
  11.     }
  12. }
import java.util.*;
public class VectorGetElement {
	public static void main(String[] args) {
	Vector<String> vector = new Vector<String>();
	vector.add("java");
        vector.add("c++");
        vector.add("python");
        vector.add("c");
        System.out.println("First element of the Vector: "+vector.get(0));
        System.out.println("Third element of the Vector: "+vector.get(2));
	}
}


Object firstElement()

We can use this method to retrieve first element from the vector

Example :

  1. import java.util.*;
  2. public class VectorFirstElement {
  3.     public static void main(String[] args) {
  4.     Vector<String> vector = new Vector<String>();
  5.     vector.add("java");
  6.         vector.add("c++");
  7.         vector.add("python");
  8.         vector.add("c");
  9.         System.out.println("First element of the Vector: "+vector.firstElement());
  10.     }
  11. }
import java.util.*;
public class VectorFirstElement {
	public static void main(String[] args) {
	Vector<String> vector = new Vector<String>();
	vector.add("java");
        vector.add("c++");
        vector.add("python");
        vector.add("c");
        System.out.println("First element of the Vector: "+vector.firstElement());
	}
}


Object elementAt(int index)

This method is very similar to first method get(int index)

It returns element at the specified index

Example :

  1. import java.util.*;
  2. public class VectorElementAt {
  3.     public static void main(String[] args) {
  4.     Vector<String> vector = new Vector<String>();
  5.     vector.add("java");
  6.         vector.add("c++");
  7.         vector.add("python");
  8.         vector.add("c");
  9.         System.out.println("Element of the Vector at index 2: "+vector.elementAt(2));
  10.     }
  11. }
import java.util.*;
public class VectorElementAt {
	public static void main(String[] args) {
	Vector<String> vector = new Vector<String>();
	vector.add("java");
        vector.add("c++");
        vector.add("python");
        vector.add("c");
        System.out.println("Element of the Vector at index 2: "+vector.elementAt(2));
	}
}

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on