04/29/101. 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.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
Linked Lists Linear collections.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
5-May-15 ArrayLists. 2 ArrayList s and arrays A ArrayList is like an array of Object s Differences between arrays and ArrayList s: Arrays have special.
L2. Necessary Java Programming Techniques (Vector) Packages  Java.util import Java.util.*; Classes  Vector Interfaces  Enumeration Java API.
For use of Cleveland State's IST410 Students only 1 Vectors and Collections.
1 L41 Collections (1). 2 OBJECTIVES  What collections are.  To use class Arrays for array manipulations.  To use the collections framework (prepackaged.
List Implementations That Use Arrays Chapter 5. 2 Chapter Contents Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation.
1 Dynamic Arrays  Why Dynamic Arrays?  A Dynamic Array Implementation  The Vector Class  Program Example  Array Versus Vector.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
25-Jun-15 Vectors. 2 Vectors and arrays A Vector is like an array of Object s Differences between arrays and Vector s: Arrays have special syntax; Vector.
Vectors. Vectors and arrays A Vector is like an array of Object s Differences between arrays and Vector s: –Arrays have special syntax; Vector s don’t.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Arrays And ArrayLists - S. Kelly-Bootle
Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic.
Topic 3 The Stack ADT.
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
ArrayList, Multidimensional Arrays
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
1.00/ Lecture 8 Arrays and Vectors. Arrays-1 Arrays are a simple data structure Arrays store a set of values of the same type – Built-in types.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
Geoff Holmes Palindrome solutions Overview Arrays Collections Enumerators Vector BitSet Stack Dictionary Hashtable Collection Classes (Chapter 19) import.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
1 Collections Framework A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain:
C arrays are limited: -they are represented by pointers (which may or may not be valid); -Indexes not checked (which means you can overrun your array);
Chapter 2 Collections. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Define the concept and terminology related.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
Title Slid CSC 444 Java Programming Arrays By Ralph B. Bisland, Jr.
List Interface and Linked List Mrs. Furman March 25, 2010.
Lecture 7 February 24, Javadoc version and author Tags These go in the comments before named classes. –Put your SS# on a separate line from the.
Building Java Programs Chapter 15 Lecture 15-2: testing ArrayIntList; pre/post conditions and exceptions reading:
ITI Introduction to Computing II Lab-5 Dewan Tanvir Ahmed University of Ottawa.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 15: Sets and Maps Java Software Structures: Designing and Using.
Utility classes 1.Vector class 2.Random class 3.Date class 4.Scanner.
CS1020 Data Structures and Algorithms I Lecture Note #6 Vector and ArrayList.
C19: Collection Classes (don’t forget to look at all the online code examples)
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Comp1004: Environments The Java Library. Coming up Recap – Encapsulation – Constructors – Loops – Arrays – ArrayList – Iterators The Java Library – Implementation.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Fundamental of Java Programming
Arrays 2/4 By Pius Nyaanga.
List Representation - Array
Java Software Structures: John Lewis & Joseph Chase
Programming in Java Lecture 11: ArrayList
Java Arrays & Strings.
Object Oriented Programming in java
L2. Necessary Java Programming Techniques
The Vector Class An object of class Vector is similar to an array in that it stores multiple values However, a vector only stores objects does not have.
L2. Necessary Java Programming Techniques
Java Utilities and Bit Manipulation
ArrayLists 22-Feb-19.
Collections Framework
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Hashing in java.util
Presentation transcript:

04/29/101

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.

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.

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 Add new element

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 Additional space allocated

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[].

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 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.

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

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

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()); }

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 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 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()); }