1 / 13

Vectors and Collections

Vectors and Collections. Wrapper Classes. I n many cases, we need to create collection of objects i.e dissimilar objects A mechanism is needed so that we can include primitive data types in such collections Wrapper classes provide that mechanism

denisepayne
Download Presentation

Vectors and Collections

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Vectors and Collections For use of Cleveland State's IST410 Students only

  2. Wrapper Classes • In many cases, we need to create collection of objects i.e dissimilar objects • A mechanism is needed so that we can include primitive data types in such collections • Wrapper classes provide that mechanism • For each primitive data type, there exists a wrapper class • Wrapper class objects are immutable For use of Cleveland State's IST410 Students only

  3. Wrapper Classes Primitive TypeWrapper Class boolean Boolean byte Byte char Character double Double float Float int Integer long Long short Short • Wrapper classes provide methods through which one can convert one type to another For use of Cleveland State's IST410 Students only

  4. Vectors • Vectors are arrays of Objects • Vector is a class in java.util • JVM takes the responsibility of expanding Vectors dynamically as the existing capacity is used up • Inheritance hierarchy for Vector java.lang.Object java.util.AbstractCollection java.util.AbstractList java.util.Vector For use of Cleveland State's IST410 Students only

  5. Instance Variables of Vector • A Vector has 3 instance variables • protected int capacityIncrement - growth size • protected int elementCount - number of elements • protected Object [] elementData - buffer to store Objects • There are 4 constructors • Vector() -can hold 10 elements to start with • Vector(int initialCapacity) • Vector(int intialCapacity, int capacityIncrement) • capacity is doubled when capacityIncrement is not specified • Vector(Collection c) For use of Cleveland State's IST410 Students only

  6. A few methods of Vector • void add(int index, Object e) - insert e at index • boolean add(Object o) - append • void addElement(Object o) - append • int capacity() - current capacity of vector • void clear() - remove all elements • boolean contains(Object e) - Test for Object e using equals • Object elementAt(int index) - return the obj at index • Object get(int index) - return Obj at index • int indexOf(Object e) - searches for first occurence • void insertElementAt(Object obj, int index) For use of Cleveland State's IST410 Students only

  7. A few methods of Vector • boolean remove(Object o) - remove first occurrence • void removeAllElements() - make the Vector empty • Object set(int index, Object e) - replace at index • int size() - returns the count of elements • Vector test example • TestVector.java • Use of a Vector to implement a stack • IntegerStack.java • StackTest.java For use of Cleveland State's IST410 Students only

  8. ArrayList • Java added a new class called ArrayList since jdk1.2 • This list can be used instead of Vectors • Our tests show that ArrayList is a faster collection class than Vectors • Each ArrayList has a capacity and this capacity is as large as the list size • The capacity grows automatically as elements are added For use of Cleveland State's IST410 Students only

  9. ArrayList • Three constructors • ArrayList() • ArrayList(Collection c) • ArrayList(int intialCapacity) • Methods to perform normal operations such as append to the list, remove from the list, check for empty list and so on • It also provides an interface to a listIterator object For use of Cleveland State's IST410 Students only

  10. ArrayList • Example of ArrayList and listIterator • ObjectList.java • SimulateQue.java For use of Cleveland State's IST410 Students only

  11. Javadoc: an aside • javadoc is a tool that comes with jdk and is a very handy tool to document your java programs • The usage of javadoc • javadoc [options] [packages|files] where Option Description -d output path Generated HTML files are stored in the directory -sourcepath directory Root directory of source file package tree -public Include only public members i output (default) -private Include both public and private members Example: javadoc -d . -public DynamicArray.java For use of Cleveland State's IST410 Students only

  12. Javadoc: an aside • As you know, java incorporates documentation tags within javadoc comments • The general organization of javadoc comments • javadoc comments should immediately precede the declaration • Start the comment with a Summary sentence and then follow with other sentences to document details of the declaration, its usage etc. • Include javadoc comment tags as necessary • Comments can include HTML tags such as <P>, <UL>, <B> etc. For use of Cleveland State's IST410 Students only

  13. Javadoc: an aside • Some common javadoc tags For use of Cleveland State's IST410 Students only

More Related