The Wayback Machine - https://web.archive.org/web/20240810111726/https://www.geeksforgeeks.org/how-to-get-first-and-last-element-from-the-vector-in-java/
Open In App

How to Get First and Last Element From the Vector in Java?

Last Updated : 17 Dec, 2020
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The first element in the Vector can be obtained using the firstElement() method of vector class that is represent util package. The last element in the Vector can be obtained using the lastElement() method of vector class that is also represent util package. Neither of these methods requires any parameters.

The first and last elements of the vector can be extracted in two ways:

  1. By directing getting the element at 0th index and size-1 index through get() method.
  2. By using firstElement() and lastElement() method.

Method 1: Getting elements at 0th and last index

We can use get() method:

  • For first element, get the element at 0th index.
  • For last element, get the element at (vector’s size-1)th index.

Java




// Java program to get the first and
// last element of vector
  
import java.util.Vector;
  
public class Example {
  
    public static void main(String args[])
    {
  
        // Create instance of Vector class.
  
        Vector<Integer> vector = new Vector<>();
  
        // Add some Element in Vector
  
        vector.add(5);
  
        vector.add(9);
  
        vector.add(0);
  
        vector.add(5);
  
        vector.add(3);
  
        System.out.println("The Vector Elements are: "
                           + vector);
  
        System.out.println(
            "The First Element of the Vector is : "
            + vector.get(0));
  
        System.out.println(
            "The Last Element of the Vector is : "
            + vector.get(vector.size() - 1));
    }
}


Output

The Vector Elements are: [5, 9, 0, 5, 3]
The First Element of the Vector is : 5
The Last Element of the Vector is : 3

Method 2: Using firstElement() and lastElement() method

These two methods do not accept anything as parameters.

  • firstElement() method gives the first element of the vector.
  • lastElement() method gives the last element of the vector.

Java




// Java program to return the first and last
// element of the vector
  
import java.util.Vector;
  
public class Example {
  
    public static void main(String args[])
    {
  
        // Create instance of Vector class.
  
        Vector<Integer> vector = new Vector<>();
  
        // Add some Element in Vector
  
        vector.add(5);
  
        vector.add(9);
  
        vector.add(0);
  
        vector.add(5);
  
        vector.add(3);
  
        System.out.println("The Vector Elements are: "
                           + vector);
  
        System.out.println(
            "The First Element of the Vector is : "
            + vector.firstElement());
  
        System.out.println(
            "The Last Element of the Vector is : "
            + vector.lastElement());
    }
}


Output

The Vector Elements are: [5, 9, 0, 5, 3]
The First Element of the Vector is : 5
The Last Element of the Vector is : 3


Similar Reads

Java Program to Get the First and the Last Element of a Linked List
A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The given task is to retrieve the first and the last element of a given linked list. Properties of a Linked ListElements are stored in a non-contiguous manner.Every element is an object which contains a pointer to the next element.Example
4 min read
Copy Elements of One Java Vector to Another Vector in Java
Vector is similar to arrays but is growable also, or we can say no fixed size is required. Previously vector was a part of legacy classes but now it is part of Collections. It also implements a List interface, so we can use any method of list interface on vectors also. Syntax : Vector&lt;Integer&gt; gfg=new Vector&lt;&gt;(); Ways To copy elements o
3 min read
Get first and last elements from ArrayList in Java
Given an array list, find the first and last elements of it. Examples: Input : aList = {10, 30, 20, 14, 2} Output : First = 10, Last = 2 Input : aList = {10, 30, 40, 50, 60} Output : First = 10, Last = 60 The last element is at index, size - 1 and the first element is stored at index 0. If we know how to get the size of ArrayList then we can get th
3 min read
Java Program to Get First or Last Elements from HashSet
The HashSet class implements the Set interface, backed by a hash table which is actually a HashMap instance. There is no guarantee made for the iteration order of the set which means that the class does not guarantee the constant order of elements over time. This class permits the null element. The class also offers constant time performance for th
3 min read
How to Get First or Last Entry from Java LinkedHashMap?
LinkedHashMap is a predefined class in Java which is similar to HashMap, containing key and its respective value unlike HashMap, In LinkedHashMap insertion order is preserved. The task is to get the first and last entry present in LinkedHashMap. Iteration to get last and first value. The first and the last entry in Map is the entry that is inserted
5 min read
Find first and last element of ArrayList in java
Prerequisite: ArrayList in Java Given an ArrayList, the task is to get the first and last element of the ArrayList in Java, Examples: Input: ArrayList = [1, 2, 3, 4] Output: First = 1, Last = 4 Input: ArrayList = [12, 23, 34, 45, 57, 67, 89] Output: First = 12, Last = 89 Approach: Get the ArrayList with elements.Get the first element of ArrayList w
2 min read
How to Add Element at First and Last Position of LinkedList in Java?
LinkedList is a part of Collection framework present inside java.util package. This class is an implementation of LinkedList data structure which is a linear data structure where the elements are not stored in a contiguous manner and every element is a separate object with a data field and address field. Now here we are given a Linked List, the tas
2 min read
Java Program to Get the Maximum Element From a Vector
Prerequisite: Vectors in Java Why We Use Vector? Till now, we have learned two ways for declaring either with a fixed size of array or size enter as per the demand of the user according to which array is allocated in memory. int Array_name[Fixed_size] ; int array_name[variable_size] ; Both ways we land up wasting memory so in order to properly util
4 min read
How to Get the Last Element from LinkedHashSet in Java?
The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained this class is used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements in the order in which they were inserted. When cyc
2 min read
Java Program to Get the First Element from LinkedHashSet
LinkedHashSet is an implementation of Set Abstract Data Type (ADT). It extends from the HashSet class which in-turn implements Set Interface. The difference between the LinkedHashSet and HashSet is the property of maintaining the element ordering. LinkedList is just a container holding sequence of elements. By definition, Set should not contain dup
3 min read
three90RightbarBannerImg