L2. Necessary Java Programming Techniques (Vector) Packages  Java.util import Java.util.*; Classes  Vector Interfaces  Enumeration Java API.

Slides:



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

Linked Lists Linear collections.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Collections Chapter Java Collection Frameworks The Java collection framework is a set of utility classes and interfaces. Designed for working with.
Chapter 81 JavaBeans JavaServer Pages By Xue Bai.
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.
Collections Sets - no duplicates Lists - duplicates allowed Maps - key / value pairs A collection is an Object which contains other Objects. There are.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 12: Stacks and Queues.
List Implementations That Use Arrays Chapter 5 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
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.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
1 Dynamic Arrays  Why Dynamic Arrays?  A Dynamic Array Implementation  The Vector Class  Program Example  Array Versus Vector.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
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.
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.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 13: Queues and Vectors.
Set, TreeSet, TreeMap, Comparable, Comparator. Def: The abstract data type set is a structure that holds objects and satifies ARC: Objects can be added.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
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.
The Java Collections Framework (JCF) Introduction and review 1.
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
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.
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
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.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
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.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
Title Slid CSC 444 Java Programming Arrays By Ralph B. Bisland, Jr.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
ArrayList JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
CS1020 Data Structures and Algorithms I Lecture Note #6 Vector and ArrayList.
Vector program patterns Vectors contain many elements, so loops are common. Counted processing // Print the first 3 elements. for (int i = 0; i < 3; i++)
Collections Dwight Deugo Nesa Matic
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.
Click to edit Master text styles Stacks Data Structure.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
Fundamental of Java Programming
The Class ArrayLinearList
Efficiency of in Binary Trees
List Representation - Array
Programming in Java Lecture 11: ArrayList
Copyright ©2012 by Pearson Education, Inc. All rights reserved
(Java Collections Framework) AbstractSequentialList
L3. Necessary Java Programming Techniques
L3. Necessary Java Programming Techniques
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.
L5. Necessary Java Programming Techniques
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Chapter 2 : List ADT Part I – Sequential List
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Presentation transcript:

L2. Necessary Java Programming Techniques (Vector) Packages  Java.util import Java.util.*; Classes  Vector Interfaces  Enumeration Java API

Make youeself understand What is Java Vector Class? Class Vector constructors Class Vector methods Interface Enumeration and its methods

Java class Vector provides the capabilities of array-like data structures that can dynamically resize themselves. At any time the Vector contains a certain number of elements which can be different types of objects and the size is less than or equal to its capacity. The capacity is the space that has been reserved for the array. If a Vector needs to grow, it grows by an increment that you specify. If you do not specify a capacity increment, the system will automatically double the size of the Vector each time additional capacity is needed. What is Java Vector Class?

Class Vector constructors public Vector(); Constructs an empty vector so that its internal data array has size 10. e.g. Vector v = new Vector(); public Vector(int initialCapacity); Constructs an empty vector with the specified initial capacity. e.g. Vector v = new Vector(1); public Vector(int initialCapacity, int capacityIncrement); Constructs an empty vector with the specified initial capacity and capacity increment. e.g. Vector v = new Vector(4, 2);

Class Vector methods public void addElement(Object obj) Adds the specified component to the end of this vector, increasing its size by one. The capacity of this vector is increased if its size becomes greater than its capacity. e.g. v.addElement(input.getText()); public boolean removeElement(Object obj) Removes the first (lowest-indexed) occurrence of the argument from this vector. If the object is found in this vector, each component in the vector with an index greater or equal to the object's index is shifted downward to have an index one smaller than the value it had previously. e.g. if (v.removeElement(input.getText())) showStatus(“Removed: ” + input.getText());

Class Vector methods public Object firstElement() Returns the first component (the item at index 0) of this vector. e.g. showStatus(v.firstElement()); public Object lastElement() Returns the last component of the vector. e.g. showStatus(v.lastElement() ); public boolean isEmpty() Tests if this vector has no components. e.g. showStatus(v.isEmptyt()? “Vector is empty” : “Vector is not empty” );

Class Vector methods public boolean contains(Object elem) Tests if the specified object is a component in this vector. e.g. if (v.contains(input.getText())) showStatus(“Vector contains: ” + input.getText()); public int indexOf(Object elem) Searches for the first occurence of the given argument, testing for equality using the equals method. e.g. showStatus(“Element is at location” + v.indexOf(input.getText()) ); public int size() Returns the number of components in this vector. e.g. showStatus(“Size is ” + v.size());

Class Vector methods public int capacity() Returns the current capacity of this vector. e.g. showStatus(“Capacity is ” + v.capacity()); public void trimToSize() Trims the capacity of this vector to be the vector's current size. If the capacity of this vector is larger than its current size, then the capacity is changed to equal the size by replacing its internal data array, kept in the field elementData, with a smaller one. An application can use this operation to minimize the storage of a vector. e.g. v.trimToSize(); public Enumeration elements() Returns an enumeration of the components of this vector. The returned Enumeration object will enumerate all items in this vector. The first item generated is the item at index 0, then the item at index 1, and so on. Enumeration 列挙, 一覧表

Interface Enumeration and its methods public abstract interface Enumeration An object that implements the Enumeration interface generates a series of elements, one at a time. Successive calls to the nextElement method return successive elements of the series. e.g. Enumeration enum = v.elements(); public Object nextElement() Returns the next element of this enumeration if this enumeration object has at least one more element to provide. public boolean hasMoreElements() Tests if this enumeration contains more elements. e.g. While (enum.hasMoreElements()) showStatus(enum.nextElement());

An example of using class Vector Run Java applet => VectorTest.htmlVectorTest.html Take a look at source code => VectorTest.javaVectorTest.java Exercise: 1. Understand and run the program. 2. Add a reverse button and it displays the elements in the vector in a reverse order.

Enter odd number of lines of input, use quit to end the program. apple orange banana grape lemon quit Number of lines: 5 Middle line: banana Lines in reverse order: lemon grape banana orange apple Lines in reverse alphabetical order: orange lemon grape banana apple Optional exercise: Write a java application program that uses Vector class and can output the result as shown in the right. Output: