Download presentation
Presentation is loading. Please wait.
1
For use of Cleveland State's IST410 Students only 1 Vectors and Collections
2
For use of Cleveland State's IST410 Students only 2 Wrapper Classes l I n many cases, we need to create collection of objects i.e dissimilar objects l A mechanism is needed so that we can include primitive data types in such collections l Wrapper classes provide that mechanism l For each primitive data type, there exists a wrapper class l Wrapper class objects are immutable
3
For use of Cleveland State's IST410 Students only 3 Wrapper Classes Primitive TypeWrapper Class booleanBoolean byteByte charCharacter doubleDouble floatFloat intInteger longLong shortShort l Wrapper classes provide methods through which one can convert one type to another
4
For use of Cleveland State's IST410 Students only 4 Vectors l Vectors are arrays of Objects l Vector is a class in java.util l JVM takes the responsibility of expanding Vectors dynamically as the existing capacity is used up l Inheritance hierarchy for Vector java.lang.Object java.util.AbstractCollection java.util.AbstractList java.util.Vector
5
For use of Cleveland State's IST410 Students only 5 Instance Variables of Vector l A Vector has 3 instance variables m protected int capacityIncrement - growth size m protected int elementCount - number of elements m protected Object [] elementData - buffer to store Objects l There are 4 constructors m Vector() -can hold 10 elements to start with m Vector(int initialCapacity) m Vector(int intialCapacity, int capacityIncrement) capacity is doubled when capacityIncrement is not specified m Vector(Collection c)
6
For use of Cleveland State's IST410 Students only 6 A few methods of Vector l void add(int index, Object e) - insert e at index l boolean add(Object o) - append l void addElement(Object o) - append l int capacity() - current capacity of vector l void clear() - remove all elements l boolean contains(Object e) - Test for Object e using equals l Object elementAt(int index) - return the obj at index l Object get(int index) - return Obj at index l int indexOf(Object e) - searches for first occurence l void insertElementAt(Object obj, int index)
7
For use of Cleveland State's IST410 Students only 7 A few methods of Vector l boolean remove(Object o) - remove first occurrence l void removeAllElements() - make the Vector empty l Object set(int index, Object e) - replace at index l int size() - returns the count of elements l Vector test example m TestVector.java l Use of a Vector to implement a stack m IntegerStack.java m StackTest.java
8
For use of Cleveland State's IST410 Students only 8 ArrayList l Java added a new class called ArrayList since jdk1.2 l This list can be used instead of Vectors l Our tests show that ArrayList is a faster collection class than Vectors l Each ArrayList has a capacity and this capacity is as large as the list size l The capacity grows automatically as elements are added
9
For use of Cleveland State's IST410 Students only 9 ArrayList l Three constructors m ArrayList() m ArrayList(Collection c) m ArrayList(int intialCapacity) l Methods to perform normal operations such as append to the list, remove from the list, check for empty list and so on l It also provides an interface to a listIterator object
10
For use of Cleveland State's IST410 Students only 10 ArrayList l Example of ArrayList and listIterator m ObjectList.java m SimulateQue.java
11
For use of Cleveland State's IST410 Students only 11 Javadoc: an aside l javadoc is a tool that comes with jdk and is a very handy tool to document your java programs l The usage of javadoc m javadoc [options] [packages|files] where Option Description -d output pathGenerated HTML files are stored in the directory -sourcepath directoryRoot directory of source file package tree -publicInclude only public members i output (default) -privateInclude both public and private members Example: javadoc -d. -public DynamicArray.java
12
For use of Cleveland State's IST410 Students only 12 Javadoc: an aside l As you know, java incorporates documentation tags within javadoc comments l The general organization of javadoc comments m javadoc comments should immediately precede the declaration m Start the comment with a Summary sentence and then follow with other sentences to document details of the declaration, its usage etc. m Include javadoc comment tags as necessary m Comments can include HTML tags such as,, etc.
13
For use of Cleveland State's IST410 Students only 13 Javadoc: an aside l Some common javadoc tags
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.