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 utilize memory optimization vectors are introduced.
Advantages Of Using Vectors
- Dynamic Size
- Rich Library Functions
- Easy To Know Size
- No Need To Pass Size
- Can be returned from a function
- By default initializes with default values
Rich Library Functions Includes
- Find An Element
- Erase An Element
- Insert An Element
Here, we use rich library functions to get a maximum element.
Note: Arrays are always passed as a pointer in arrays so another parameter you must pass the size of the array but that is not required in the case of vectors.
Syntax :
In the case of Arrays
type function_Name(type arrayName[], type sizeOfArray) ;
In the case of Vectors
type function_Name(vector<type> vectorName) ;
Considering An Example given a vector, the task is to find the maximum element.
Examples:
Input: v1={1,2,3,4,5}
Output: 5
Input: v2={7,50,0,67,98}
Output: 98
Method 1: Using a Predefined Function
- First, we will initialize a vector lets say v, then we will store values in that vector.
- After that, we will call the predefined method called max() defined in class java.util.Collections.
- Print the max element.
Below is the implementation of the above approach.
Java
// Java Program to find maximum element // in a vector using predefined method import java.io.*; // Importing Vector Class import java.util.Collections; // Importing Vector Class import java.util.Vector; class GFG { // Main Method public static void main(String[] args) { // initializing a Vector Vector<Integer> v = new Vector<Integer>(); // adding values to the Vector v.add( 7 ); v.add( 50 ); v.add( 0 ); v.add( 67 ); v.add( 98 ); // finding the largest element int n = Collections.max(v); // printing the largest element System.out.println( "The maximum value present in Vector is : " + n); } } |
Output :
The maximum value present in Vector is : 98
Worst Case Time Complexity: O(n) where n is the number of elements present in the vector.
Method 2: Comparing each element present in Vector
- First, we will initialize a vector lets say v, then we will store values in that vector.
- Next, we will take a variable, let us say maxNumber and assign the minimum value possible.
- Traverse till the end of vector and compare each element of a vector with maxNumber.
- If the element present in the vector is greater than maxNumber, then update maxNumber to that value.
- Print maxNumber.
Below is the implementation of the above approach.
Java
// Java program to find largest element // present in Vector via comparison import java.io.*; // Importing Vector Class import java.util.Vector; // Importing Iterator Class import java.util.Iterator; class GFG { // Main Method public static void main(String[] args) { // initializing vector of Integer type Vector<Integer> v = new Vector<Integer>(); // Adding elements in vector v.add( 1 ); v.add( 2 ); v.add( 3 ); v.add( 4 ); v.add( 5 ); // Assigning min value possible int maxValue = Integer.MIN_VALUE; // Creating an iterator to traverse through vector // in the beginning itr will point to index just // before first element Iterator itr = v.iterator(); // See if there is any next element while (itr.hasNext()) { // Moving iterator to next element int element = (Integer)itr.next(); // Comparing if element is larger than maxValue if (element > maxValue) { // Update maxValue maxValue = element; } } // Print maxVaue System.out.println( "The largest element present in Vector is : " + maxValue); } } |
Output :
The largest element present in Vector is : 5
Time Complexity: O(n) where n is the number of elements present in the vector.