0% found this document useful (0 votes)
3 views

Vector in Java

The document provides an overview of the Vector class in Java, detailing four methods for initialization and various methods for manipulating Vector elements. It includes examples of adding, removing, and accessing elements, as well as iterating through the Vector using different techniques. Additionally, it covers cloning a Vector and converting it to an array.

Uploaded by

vish21cs161
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Vector in Java

The document provides an overview of the Vector class in Java, detailing four methods for initialization and various methods for manipulating Vector elements. It includes examples of adding, removing, and accessing elements, as well as iterating through the Vector using different techniques. Additionally, it covers cloning a Vector and converting it to an array.

Uploaded by

vish21cs161
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Vector in Java

-> Four ways to initialization

Vector<datatype> var = new Vector<datatype>(); //default size 10


Vector<datatype> var = new Vector<datatype>(int size);
Vector<datatype> var = new Vector<datatype>(int size, int increment);
Vector<datatype> var = new Vector<Collection c>;

-> Methods

var.add(element)
var.add(index, element) //it will append the element to the particular index
var.insertElement(element,index)
var.remove(index)
var.remove(Datatype.valueOf(element)) // it will remove the forst occurrence of the
element
var.size()
v.setSize(int) // null value for empty spaces
var.get(index) // get the value for the particular index
var.set(index, new_value) // relpace the index value
var.setElementAt(element, index)
var.capacity()
var.addAll(c) // add the collections
var.addElement(element)
var.removeElemet(element)
var.removeAll(collection)
var.clear()
var.constains(element) // True or False
var1.containsAll(va2) // Checks var2 present in var1
var.elementAt(index)
var1.equals(var2)
var.firstElement()
var.lastElement()
var.indexOf(element)
var.lastIndexOf(element)
var.removeRange(start_index, end_index+1);
var.sort()
var2 = var1.subList(start, end+1);

->Reference

// Clone the Vector


Object copy_vector = vector.clone();

// Cast the cloned object back to Vector


Vector<Integer> clonedVector = (Vector<Integer>) copy_vector;

// Print and display the cloned vector elements


for (Integer element : clonedVector)
{
System.out.println(element);
}

-> Reference
Enumeration enu = vec_tor.elements();
// Displaying the Enumeration

while (enu.hasMoreElements())
{
System.out.println(enu.nextElement());
}

-> Reference

var.forEach((n) -> System.out.println(n));

-> Reference

Iterator value = variable.iterator();


// Condition holds true till there is single element
// remaining using hasNext() method
while (value.hasNext())
{
// Displaying the values
System.out.println(value.next());
}

ListIterator listItr = variable.listIterator();

// Forward iterations
System.out.println("Forward Traversal:");
while (listItr.hasNext())
{
System.out.println(listItr.next());
}

// Backward iterations
System.out.println("\nBackward Traversal:");
while (listItr.hasPrevious())
{
System.out.println(listItr.previous());
}

->Reference

Object[] arr = vec_tor.toArray();


System.out.println("The array is:");
for (int j = 0; j < arr.length; j++)
System.out.println(arr[j]);

You might also like