L2. Necessary Java Programming Techniques

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.
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.
L2. Necessary Java Programming Techniques (Vector) Packages  Java.util import Java.util.*; Classes  Vector Interfaces  Enumeration Java API.
Collections Sets - no duplicates Lists - duplicates allowed Maps - key / value pairs A collection is an Object which contains other Objects. There are.
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.
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.
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.
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.
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
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.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
© 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.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
1 CS162: Introduction to Computer Science II Abstract Data Types.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fundamental of Java Programming
Linked Lists Chapter 5 (continued)
The Class ArrayLinearList
Vectors Holds a set of elements, like an array
ARRAYLIST AND VECTOR.
List Representation - Array
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
ArrayLists.
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
Java Arrays & Strings.
Object Oriented Programming in java
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
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
Linked Lists Chapter 5 (continued)
ArrayLists 27-Apr-19.
Chapter 2 : List ADT Part I – Sequential List
Hashing in java.util
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Linked Lists Chapter 5 (continued)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Presentation transcript:

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

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

What is Java Vector Class? 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.

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.html Take a look at source code => VectorTest.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.