To create a Java vector, you simply need to import the Java.util.Vector and then create a Vector using the syntax below: Vector<Type> vector = new Vector<>() where the Type of the Vectors can either be a string or an integer. add(element)- adds elements to a vector add(index, element)- adds elements to a specific position addAll(vector)- adds elements of the vector to another vector get(index)-returns elements that the index has specified iterator()- returns an iterator object to access vector elements sequentially. Remove(index)- removes the specified element removesAll()- all elements are removed clear()- clears all elements Create Simple Vector Example in Java import java.util.Vector; public class VectorExample { public static void main(String[] args) { Vector<String> fish= new Vector<>(); // Using the add() method fish.add("Shark"); fish.add("Dolphines"); // Using index number fish.add(2, "mudgfish"); System.out.println("Vector: " + fish); // Using addAll() Vector<String> amphibians = new Vector<>(); amphibians.add("Lizards"); amphibians.addAll(amphibians); System.out.println("New Vector: " + amphibians); } } Output: Vector: [Shark,Dolphines,mudgfish] New Vector: [Lizards.Lizards]
Vector Methods
Article Contributed By :
|
|
|
|
70 Views |