Vectors & Arraylist
Vectors & Arraylist
GENERICS
BY:WASEEM AHMAD
REG::BCS-3A
ARRAY LIST:
• Arraylist supports dynamic arrays that can grow as needed.
• The arraylist class extends abstractlist and implements the list interface.
Syntax:
ArrayList<?> name=new ArrayList<?>();//creating the arraylist object
CONSTRUCTORS:
CONSTRUCTOR DISCREAPTION
ArryList() construct an empty list with intial capacity of 10.
Syntax:
ArrayList <class> A=new ArrayList<class>();
OR
ArrayList A=new ArrayList ();
Arraylist(int Intialcapacity) Construct an empty list with intial capacity (given by programer).
Syntax:
ArrayList <class> A=new ArrayList<class>(20);
ArrayList(Collection c) This constructor builds an array list that is initialized with the
elements of the collection C.
Syntax:
ArrayList <class> A=new ArrayList<class>(list);
COMMON METHODS IN JAVA
METHOD NAME DESCRIPTION
Boolean add(object o) Adding object to the list
void add(int index, Object element) Adding at some particular index of the list
Boolean addall(collection c) Adding all the element of another list
Boolean addall(int index, collection c) Inserting a C(list) at some particular index
Int Size() Check the number of elements in list present
Object remove(int index) Remove an object from the list at some index
Object get(int index) Retrive the element from list at particular index
Object set(int index, object element) Replacing the element of some index by some another element
VECTORS
• The vector class implements a growable array of objects. Vectors basically falls in legacy classes but
now it is fully compatible with collections.
• The vector class implements a growable array of objects. (Like an Arraylist)
Legacy class:
When collection came these classes reengineered to support the
collection interfaces. These old classes are known are legacy
classes. Vector is one of them.
Syntax:
Vector <CLASS Name> V=new Vector<Class Name>();
CONSTRUCTORS:
Constructor Discreption
Vector() Creates a default vector of initial capacity is 10.
Syntax:
Vector <Class> V=new Vector<Class>();
Vector(int size, int incr) Creates a vector whose initial capacity is specified by size and
increment is specified by incr.
Syntax:
Vector <Class> V=new Vector<Class>(20,5);
IMPORTANT METHODS IN VECTOR CLASS
Vector Methods:
Vector contains many legacy methods that are not part of collection framework which we will discuss below
with examples in java.
Methods Description
clone clone() This method gives a duplicate copy of the whole vector.
boolean containsAll(Collection c) This method returns true if all the elements of one collection is
present in calling collection, or false if not
void copyInto(Object anArray) This method copies all the elements vector to the array specified
array in argument
ADVANTAGES & DISADVANTAGS
ADVANTAGES DISADVANTAGES
Vector is efficient in insertion, deletion and to A vector is an object, memory consumption is more.
increase the size.
Vector is synchronized Speed is slower than arraylist, which is not synchronized
It means that multiple threads can modify the
Vector in parallel without risk of data corruption.
Multiple objects can be stored. Vector only stored the objects .(not primitive)
Example:
Vector v=new Vector();
Vector is used the method which is defined in v.Add(2);//error becouse of no Genrics mention
Vector class but also the method of Collection Correct:
framework(Interface) Vector <Integer> v=new Vector<Integer>();
v.Add(2);
v.Add(8);
VECTOR EXAMPLES:1
Import java.Util.Vector;
v.Addelement(9);
v.Addelement(29);
System.out.Println("the size"+v.size());
System.Out.Println("the capacity"+v.capacity());
Iterator<integer> l=v.iterator();
v.Addelement(9);
v.Addelement(29);
System.out.Println("the size"+v.size());
System.Out.Println("the capacity"+v.capacity());
Iterator<integer> l=v.iterator();
ArrayList gives better performance as it is non- Vector operations gives poor performance .
synchronized.
ArrayList is not a legacy class. It is introduced in JDK Vector is a legacy class.
1.2.
Array list is the part of collection frame work. The vector was not the part of collection framework, it
has been included in collections later
Iterator and for-each loop is use to treversed the Enumaration interface is used to treversed the vectors.
arraylist.
GENERICS IN JAVA:
Problem:
Def: LISt list = new arraylist();
List.Add("abc");
The process of injecting homogeneous
List.Add(new integer(5)); //OK
object in to container of (JCF) JAVA
Collection frame work. For(object obj : list)
{ //Type casting leading to classcastexception at
runtime
string str=(string) obj;
}
GENERICS IN JAVA:
• Generics in java were introduced as one of features in JDK 5.
• Generics was first introduced in java5. Now it is one of the most profound feature of java
programming language.
Syntax:
List<String> l1=new ArrayList<String>();
ADVANTAGES OF GENERICS :
• A.ADD(“WAJED”); • A.ADD(“WAJED”);
• A.ADD(“ALI”); • A.ADD(“ALI”);
• GENERIC TYPES
• Definition: “A generic type is a generic class or interface that is parameterized over types.”
• Java generic type naming convention helps us understanding code easily and having a naming convention is one of the
best practices of java programming language. So generics also comes with it’s own naming conventions.
• We can define our own classes with generics type. A generic type is a class or interface that is
parameterized over types. We use angle brackets (<>) to specify the type parameter.
Syntax:
Class demo<T>
{......
......
}
Main()
{
demo <String> d=new demo<>();
}
EXAMPLE
PUBLIC CLASS GENERICSTYPE<T>
Private T R;
R=T1;
SET("pankaj"); //valid
}
GENERINC METHOD
• GENERIC METHODS
• You can also create generic methods that can be called with different types of
arguments. Based on the type of arguments passed to generic method, the compiler
handles each method.
• The syntax for a generic method includes a type-parameter inside angle brackets,
and should appear before the method's return type.
Syntax:
Public <type> void Method(T t1,T t2)
{
.........
.........
}