java.util.Vector.elementAt() Method


Description

The elementAt(int index) method is used to get the component at the specified index/location of the vector.

Declaration

Following is the declaration for java.util.Vector.elementAt() method

public Object elementAt(int index)

Parameters

index − This is an index into this vector.

Return Value

It returns the component at the specified index.

Exception

ArrayIndexOutOfBoundsException − if the index is negative or not less than the current size of this Vector object given.

Example

The following example shows the usage of java.util.Vector.elementAt() method.

package com.tutorialspoint;

import java.util.Vector;

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

      // create an empty Vector vec with an initial capacity of 4      
      Vector<Integer> vec = new Vector<Integer>(4);

      // use add() method to add elements in the vector
      vec.add(4);
      vec.add(3);
      vec.add(2);
      vec.add(1);

      // let us print the element at 1st position in the vector
      System.out.println("Element at 1st position :- "+vec.elementAt(1));
   }    
}

Let us compile and run the above program, this will produce the following result.

Element at 1st position :- 3
java_util_vector.htm
Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements