0% found this document useful (0 votes)
6 views1 page

PR 5 Vector

The document contains a Java program that demonstrates the use of the Vector class. It initializes a vector with fruit names, modifies its elements, and showcases various operations such as adding, retrieving, setting, removing elements, and iterating through the vector. Finally, it checks for the presence of an element and clears the vector, displaying the results at each step.

Uploaded by

figma426
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

PR 5 Vector

The document contains a Java program that demonstrates the use of the Vector class. It initializes a vector with fruit names, modifies its elements, and showcases various operations such as adding, retrieving, setting, removing elements, and iterating through the vector. Finally, it checks for the presence of an element and clears the vector, displaying the results at each step.

Uploaded by

figma426
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

*;
class pr5Vector{
public static void main(String[]args){
Vector<String>vector=new Vector<>(5);
vector.add("Apple");
vector.add("Banana");
vector.add("Grapes");
vector.add("Mango");
vector.add("Pineapple");
vector.add(2,"BlueBerry");
System.out.println("Vectors elements:" + vector);
System.out.println("Element at index 2:"+vector.get(2));
vector.set(1,"Grapes");
System.out.println("Vector after modification:"+vector);
System.out.println("Size of vector:"+vector.size());
vector.remove("Pineapple");
System.out.println("Vector after removing 'Pineapple':"+vector);
System.out.println("vector elements using iterator:");
Iterator<String>iterator=vector.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
System.out.println("Vector elements using for-each loop:");
for (String fuit:vector){
System.out.println(fuit);
}
if(vector.contains("Apple")){
System.out.println("Apple is present in the vector.");
}
vector.clear();
System.out.println("Vector after clearing:"+vector);
}
}

You might also like