Download presentation
Presentation is loading. Please wait.
Published byGarry Godfrey Briggs Modified over 9 years ago
1
04/29/101
2
2 Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed using integer index. Vectors always stores the objects. Vector class is in the java.util package.
3
04/29/103 Constructors in Vector class... Basically, there are four constructors defined in the vector class. ConstructorDescription Vector() The constructor creates a default vector containing no elements Vector(int capacity) Creates an empty vector with the initial capacity specified by the parameter ‘capacity’ Vector(int capacity,int increment) Creates an empty vector with initial capacity and the increment specified by the parameters ‘capacity’ and ‘increment’ respectively Vector(Collection c)Creates a vector containing the elements of the collection c. The elements are accessed in the order returned by the collection’s iterator.
4
02/10/104 Memory allocation for vectors... All vectors start with an initial capacity defined by the programmer. Vector v=new Vector(5,3); After this initial capacity is reached, the next time that attempt to store an object in the vector, the vector automatically allocates space for that object + extra room for additional objects. 01234 34815 01234 9 Add new element
5
02/10/105 Memory allocation for vectors (Cont)... The amount of extra space allocated during each reallocation is determined by the increment that specified when creating the vector. If the increment is not specified, the vector size is doubled by each allocation cycle. 34815 01234 9 5 Additional space allocated
6
04/29/106 Methods defined in Vector class... MethodDescription void addElement(element) The object specified by the parameter ‘element’ is added to the vector. int capacity()Returns the capacity of the vector boolean contains(element) Returns true if element is contained by the vector. Returns false other wise. void copyInto(Object arr[])The elements contained in the invoking vector are copied into the array specified by the parameter arr[].
7
04/29/107 Methods defined in Vector class (cont)... MethodDescription Element elementAt(int index) Returns the element specified by the index void ensureCapacity(int min) Sets the minimum capacity of the vector to the size specified by ‘min’. Element FirstElement() Returns the first element in the vector. int indexOf(Object element) Returns the index of first occurrence of the element. If the object is not in the vector, -1 is returned. void insertElementAt(Element element, int index) Adds the ‘element’ to the vector at the location specified by the ‘index’
8
8 Methods defined in Vector class (Cont)... MethodDescription boolean isEmpty() Returns true if the vector is empty, and returns false if it contains one or more elements. Element lastElement() Returns the last element of the vector int lastIndexOf(Object element) Returns the index of the last occurrence of element. If the object is not in the vector, then returns -1 void removeAllElements()Empties the vector. After executing this method, the size of the vector becomes zero.
9
04/29/109 Methods defined in Vector class (Cont)... MethodDescription boolean removeElement(Object element) Removes element from the vector. If more than one instance of the Object exists in the vector, then it is the first one that is removed. Returns true if successful and false if the object is not found. void removeElement(int index) Removes the element at the location specified by the index void setElementAt(Element element, int index) The ‘element’ is assigned to the location specified by the index
10
04/29/1010 Methods defined in Vector class (Cont)... MethodDescription void setSize(int size) Sets the number of elements in the vector to the ‘size’. If the new size is less than the old size, elements are lost. If the new size is larger than the old size, null elements are added. int size() Returns the number of elements currently in the vector void trimToSize () Sets the vectors capacity equal to the number of elements that it currently holds. Object get(int index)Retrieves the element at the location specified by the index
11
04/29/1011 Example (1)... import java.util.Vector; class Vector_Example { public static void main(String arg[]) { Vector v=new Vector(5,3); v.addElement(4); v.addElement(“Kamal”); v.addElement(90.5); System.out.println(v.size()); } import java.util.Vector; class Vector_Example { public static void main(String arg[]) { Vector v=new Vector(5,3); v.addElement(4); v.addElement(“Kamal”); v.addElement(90.5); System.out.println(v.size()); }
12
04/29/1012 Example (2)... import java.util.Vector; class Vector_Example { public static void main(String arg[]) { Vector v=new Vector (5,3); v.addElement(4); v.addElement(5); v.addElement(9); System.out.println(v.get(2)); } import java.util.Vector; class Vector_Example { public static void main(String arg[]) { Vector v=new Vector (5,3); v.addElement(4); v.addElement(5); v.addElement(9); System.out.println(v.get(2)); }
13
13 Iterator interface... The iterator inteface provides a standard means of iterating through a list of elements. There some methods defined by the iterator interface. - public boolean hasNext() This method determines whether the structure contains any more elements. - public Object next() Retrieves the next element in the structure. If there are no more elements, next() with throw a NoSuchElementException exception.
14
14 Example (3)... Vector v=new Vector (5,3); v.addElement(4); v.addElement(5); v.addElement(9); //Defines an Iterator object for vector by method iterator() Iterator it=v.iterator(); while(it.hasNext()) { System.out.println(it.next()); } Vector v=new Vector (5,3); v.addElement(4); v.addElement(5); v.addElement(9); //Defines an Iterator object for vector by method iterator() Iterator it=v.iterator(); while(it.hasNext()) { System.out.println(it.next()); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.