Insert all elements of other
Collection to Specified Index of Java Vector
import
java.util.ArrayList;
import java.util.Vector;
public class
InsertAllElementsOfOtherCollectionToVectorExample
{
public static void
main(String[] args)
{
Vector v = new Vector();
v.add("1");
v.add("2");
v.add("3");
ArrayList arrayList =
new ArrayList();
arrayList.add("4");
arrayList.add("5");
v.addAll(1,arrayList);
System.out.println("After
inserting all elements of ArrayList at index 1,
Vector
contains..");
for(int i=0;
i
System.out.println(v.get(i));
}
}
Iterate through elements Java
Vector using Iterator
import java.util.Vector;
import
java.util.Iterator;
public class
IterateThroughVectorUsingIteratorExample
{
public static void
main(String[] args)
{
Vector v = new Vector();
v.add("1");
v.add("2");
v.add("3");
v.add("4");
v.add("5");
Iterator itr =
v.iterator();
System.out.println("Iterating
through Vector elements...");
while(itr.hasNext())
System.out.println(itr.next());
}
}
Iterate through elements Java
Vector using List Iterator
import java.util.Vector;
import
java.util.ListIterator;
public class
IterateThroughVectorUsingListIteratorExample
{
public static void
main(String[] args)
{
Vector v = new Vector();
v.add("1");
v.add("2");
v.add("3");
v.add("4");
v.add("5");
ListIterator itr =
v.listIterator();
System.out.println("Iterating
through Vector elements in forward direction...");
while(itr.hasNext())
System.out.println(itr.next());
System.out.println("Iterating
through Vector elements in backward direction...");
while(itr.hasPrevious())
System.out.println(itr.previous());
}
}
Remove all elements from Java
Vector
import
java.util.Vector;
public class
RemoveAllElementsOfVectorExample
{
public static void
main(String[] args)
{
Vector v = new Vector();
v.add("1");
v.add("2");
v.add("3");
System.out.println("Size
of Vector before removing elements : " + v.size());
v.clear();
System.out.println("Size of Vector after removing elements : " +
v.size());
}
}
Remove an element from specified
index of Java Vector
import java.util.Vector;
public class
RemoveElementFromVectorByIndexExample
{
public static void
main(String[] args)
{
Vector v = new Vector();
v.add("1");
v.add("2");
v.add("3");
v.add("4");
v.add("5");
Object obj =
v.remove(1);
System.out.println(obj +
" is removed from Vector");
System.out.println("Vector
contents after remove call...");
for(int index=0; index
< v.size(); index++)
System.out.println(v.get(index));
v.removeElementAt(2);
System.out.println("Vector
contents after removeElementAt call...");
for(int index=0; index
< v.size(); index++)
System.out.println(v.get(index));
}
}
No comments:
Post a Comment