Java Vector.elementAt(int index) Method

 Posted On  | Yashwant Chavan 

In this tutorial, you will learn about Java Vector.elementAt(int index) Method. This method returns the element at the specified index.

syntax

Returns the component at the specified index.

E java.util.Vector.elementAt(int index)

Parameters

index an index into this vector.

Return

returns the element at the specified index.

Exceptions

throws ArrayIndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

Get element from specified index in Vector

  • Create Vector instance using new Vector().
  • Add some elements in the Vector.
  • Use elementAt() method to get the Vector element at the specified index.
package com.technicalkeeda.app;

import java.util.Vector;

public class VectorElementAt {
    public static void main(String[] args) {

        Vector < String > colors = new Vector < String > ();
        colors.add("Red");
        colors.add("Green");
        colors.add("Blue");
        colors.add("Pink");

        System.out.println("Element at 0th position:- " + colors.elementAt(0));
        System.out.println("Element at 2nd position:- " + colors.elementAt(2));

    }
}

Output

Element at 0th position:- Red
Element at 2nd position:- Blue


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0329 seconds.