The
Vector
class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of aVector
can grow or shrink as needed to accommodate adding and removing items after the Vector
has been created.Each vector tries to optimize storage management by maintaining a
capacity
and a capacityIncrement
. The capacity
is always at least as large as the vector size; it is usually larger because as components are added to the vector, the vector's storage increases in chunks the size of capacityIncrement
. An application can increase the capacity of a vector before inserting a large number of components; this reduces the amount of incremental reallocation.As of the Java 2 platform v1.2, this class has been retrofitted to implement List, so that it becomes a part of Java's collection framework. Unlike the new collection implementations, Vector is synchronized.
import java.util.*;
class co
{
public static void main(String arg[])
{
Vector v=new Vector();
v.addElement("a");
for(int i=0;i<10;i++)
{
v.addElement(new Integer(i));
}
System.out.println(v);
System.out.println(v.capacity()); // 10
v.add("b");
System.out.println(v.capacity()); // 20
}
}
******************************************************************
import java.util.*;
class co1
{
public static void main(String[] args)
{
ArrayList a1=new ArrayList();
a1.add(new Integer(10));
a1.add("hello");
a1.add(new Integer(20*5));
a1.add(new Integer(30));
System.out.println(a1); // here ArryList toString method called
// By using iterator
Iterator i1=a1.iterator();
while(i1.hasNext())
System.out.println(i1.next());
}
}
*****************************************************************************
import java.util.ArrayList;
public class co2
{ public static void main(String[] args)
{
//create an ArrayList object
ArrayList arrayList = new ArrayList();
//Add elements to Arraylist
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
/* To add an element at the specified index of ArrayList use void add(int index, Object obj) method. This method inserts the specified element at the specified index in the ArrayList. */
arrayList.add(1,"INSERTED ELEMENT");
/* Please note that add method DOES NOT overwrites the element previously at the specified index in the list. It shifts the elements to right side and increasing the list size by 1. */
System.out.println("ArrayList contains...");
//display elements of ArrayList
for(int index=0; index < arrayList.size(); index++)
System.out.println(arrayList.get(index));
}
}
***********************************************************************
//AppendAllElementsOfOtherCollectionToArrayListExample
import java.util.ArrayList;
import java.util.Vector;
public class co3
{
public static void main(String[] args)
{
//create an ArrayList object
ArrayList arrayList = new ArrayList();
//Add elements to Arraylist
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
//create a new Vector object
Vector v = new Vector();
v.add("4");
v.add("5");
/* To append all elements of another Collection to ArrayList use boolean addAll(Collection c) method. It returns true if the ArrayList was changed by the method call. */
//append all elements of Vector to ArrayList
arrayList.addAll(v);
//display elements of ArrayList
System.out.println("After appending all elements of Vector, ArrayList contains..");
for(int i=0; i<arrayList.size(); i++)
System.out.println(arrayList.get(i));
}}
/*Output would beAfter appending all elements of Vector, ArrayList contains..12345*/
**********************************************************************************
//CopyElementsOfArrayListToArrayExample
import java.util.ArrayList;
public class co4
{
public static void main(String[] args)
{
//create an ArrayList object
ArrayList arrayList = new ArrayList();
//Add elements to ArrayList
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("4");
arrayList.add("5");
/* To copy all elements of java ArrayList object into array use Object[] toArray() method. */
Object[] objArray = arrayList.toArray();
//display contents of Object array
System.out.println("ArrayList elements are copied into an Array. Now Array Contains..");
for(int index=0; index < objArray.length ; index++) System.out.println(objArray[index]); }}
**********************************************************************************
//GetSizeOfArrayListExample
import java.util.ArrayList;
public class co5
{
public static void main(String[] args)
{
//create an ArrayList object
ArrayList arrayList = new ArrayList();
//Add elements to Arraylist using
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
//To get size of Java ArrayList use int size() method
int totalElements = arrayList.size();
System.out.println("ArrayList contains...");
//loop through it
for(int index=0; index < totalElements; index++) System.out.println(arrayList.get(index));
}
}
************************************************************************
//GetSubListOfJavaArrayListExample
import java.util.ArrayList;
import java.util.List;
public class co6
{
public static void main(String[] args)
{
//create an ArrayList object
ArrayList arrayList = new ArrayList();
//Add elements to Arraylist
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("4");
arrayList.add("5");
/* To get a sub list of Java ArrayList use List subList(int startIndex, int endIndex) method. This method returns an object of type List containing elements from startIndex to endIndex - 1. */
List lst = arrayList.subList(1,3);
//display elements of sub list.
System.out.println("Sub list contains : ");
for(int i=0; i< lst.size() ; i++)
System.out.println(lst.get(i));
/* Sub List returned by subList method is backed by original Arraylist. So any changes made to sub list will also be REFLECTED in the original Arraylist. */
//remove one element from sub list
Object obj = lst.remove(0);
System.out.println(obj + " is removed from sub list");
//print original ArrayList
System.out.println("After removing " + obj + " from sub list, original ArrayList contains : "); for(int i=0; i< arrayList.size() ; i++)
System.out.println(arrayList.get(i));
}
}
No comments:
Post a Comment