Home » Java programming language

Java Vector get() Method with Example

Vector Class get() method: Here, we are going to learn about the get() method of Vector Class with its syntax and example.
Submitted by Preeti Jain, on March 17, 2020

Vector Class get() method

  • get() method is available in java.util package.
  • get() method is used to return the object that exists at the given indices in this Vector.
  • get() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • get() method may throw an exception at the time of returning indexed element.
    ArrayIndexOutOfBoundsException: This exception may throw when the given parameter is not in a range.

Syntax:

    public Element get(int indices);

Parameter(s):

  • int indices – represents the position of the retrieved element.

Return value:

The return type of the method is Element, it returns indexed element.

Example:

// Java program to demonstrate the example 
// of Element get(int indices) method 
// of Vector 

import java.util.*;

public class getOfVector {
    public static void main(String[] args) {
        // Instantiates a Vector object  with
        // initial capacity of "10"
        Vector < String > v = new Vector < String > (10);

        // By using add() method is to add the
        // elements in this v
        v.add("C");
        v.add("C++");
        v.add("JAVA");

        // Display Vector
        System.out.println("v: " + v);

        // By using get(1) method is to
        // get the value at the given indices
        // "1"
        Object ele = v.get(1);

        // Display returned element at the
        // given indices
        System.out.println("v.get(1): " + ele);
    }
}

Output

v: [C, C++, JAVA]
v.get(1): C++


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.