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.

Slides:



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

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Chapter 81 JavaBeans JavaServer Pages By Xue Bai.
OO Programming Objectives for today: Casting Objects Introduction to Vectors The instanceof keyword.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
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.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007.
For use of Cleveland State's IST410 Students only 1 Vectors and Collections.
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.
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.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
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.
Vectors Cmput Lecture 4 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is based on code from.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 13: Queues and Vectors.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
04/29/ 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.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
Java – Part II Lecture Notes 4. Arrays An array is a data structure that groups and organizes data l Array is a list of values (int, double, aggregates)
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.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
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.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
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);
© 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Stack Implementations Chapter 6 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Data Structure Specification  Language independent  Abstract Data Type  C++  Abstract Class.
1 Sorting b Sorting is the process of arranging a list of items into a particular order b There must be some value on which the order is based b There.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
1 Objects for Organizing Data -- Introduction zAs our programs get more sophisticated, we need assistance organizing large amounts of data zChapter 6 focuses.
1 Lecture 8 b Data Structures b Abstraction b The “Structures” package b Preconditions and postconditions b Interfaces b Polymorphism b Vector class b.
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.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
An Array-Based Implementation of the ADT List
Fundamental of Java Programming
Lecture 10 Collections Richard Gesick.
The Class ArrayLinearList
List Representation - Array
Programming in Java Lecture 11: ArrayList
Lecture 10 List Richard Gesick.
Linked List Intro CSCE 121 J. Michael Moore.
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
EE 422C Sets.
Java Arrays & Strings.
Object Oriented Programming in java
L2. Necessary Java Programming Techniques
Collections Not in our text.
Vectors.
L2. Necessary Java Programming Techniques
Java Utilities and Bit Manipulation
ArrayLists 22-Feb-19.
Stacks public interface Stack { public boolean empty();
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
ArrayLists 27-Apr-19.
Chapter 2 : List ADT Part I – Sequential List
Linked List Intro CSCE 121.
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Arrays.
CHAPTER 3: Collections—Stacks
Presentation transcript:

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 the indexing syntax that arrays have The methods of the Vector class are used to interact with the elements of a vector The Vector class is part of the java.util package

The Vector Class An important difference between an array and a vector is that a vector can be thought of as a dynamic, able to change its size as needed Each vector initially has a certain amount of memory space reserved for storing elements If an element is added that doesn't fit in the existing space, more room is automatically acquired

The Vector Class The Vector class is implemented using an array Whenever new space is required, a new, larger array is created, and the values are copied from the original to the new array To insert an element, existing elements are first copied, one by one, to another position in the array Therefore, the implementation of Vector in the API is not very efficient for inserting elements

Creating a Vector Constructors public Vector(int initialCapacity, int increment); public Vector(int initialCapacity) public Vector(); // gives an initial capacity of 10

Accessing an Element public void setElementAt(Object newEelement, int index); Analogous to: Array[index] = newElement; public void elementAt(int index); Analogous to: Array[index] In both cases, index must be greater than zero and less than the current size of the vector

Adding Elements public void addElement(Object newElement) Adds specified element to the end of the vector and increases the size by one. Also increases the capacity if necessary. public void insertElementAt(Object newElement, int index) Inserts newElement at the specified index position and shifts the other existing elements to make room public void setElementAt(Object NewElement, int index) Overwrites the existing element

Removing Elements public void removeElementAt(int index) Removes element at the specified index and shifts all other elements down public boolean removeElement(Object theElement) Removes first occurrence of theElement and shifts all other elements down. Returns true if theElement is found and removed public void removeAllElements()

Memory Management public boolean isEmpty() public int size() Returns true is size==0; public int size() Returns number of elements public int capacity() Returns capacity