CSC 205 – Java Programming II Lecture 25 March 8, 2002.

Slides:



Advertisements
Similar presentations
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
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.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
15-Jun-15 Lists in Java Part of the Collections Framework.
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.
Algorithm Programming Containers in Java Bar-Ilan University תשס " ו by Moshe Fresko.
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 17 Advanced Java Concepts Data Structure Support.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
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.
Collections The objectives of this chapter are: To outline the Collections infrastructure in Java To describe the various collection classes To discuss.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
1 ADTs, Collection, Iterable/Iterator Interfaces Collections and the Java Collections API The Collection Interface and its Hierarchy The Iterable and Iterator.
CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList.
12-Jul-15 Lists in Java Part of the Collections Framework.
1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
(c) University of Washington14-1 CSC 143 Java Collections.
The Java Collections Framework Chapters 7.5. Outline Introduction to the Java Collections Framework Iterators Interfaces, Abstract Classes and Classes.
CSE 143 Lecture 4 ArrayList Reading: 10.1 slides created by Marty Stepp
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.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
The Java Collections Framework Based on
CHAPTER 5 ArrayLists.
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
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.
Java 2 Collections Bartosz Walter Software Engineering II.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
CS Ananda Gunawardena.  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
List Interface and Linked List Mrs. Furman March 25, 2010.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
List data type(ADT). Lists Elements : a 1,a 2,a 3,… a i-1,a i, a i+1,…a n Null List contains: 0 elements Types of Operations on list 1.Insertion 2.Deletion.
CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
Collections Dwight Deugo Nesa Matic
Object Oriented Programming in Java Habib Rostami Lecture 7.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
CS 151: Object-Oriented Design December 3 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Java Collections CHAPTER 3-February-2003
Data Structures Lakshmish Ramaswamy.
Implementing ArrayList Part 1
TCSS 143, Autumn 2004 Lecture Notes
Programming in Java Lecture 11: ArrayList
(Java Collections Framework) AbstractSequentialList
Collections in Java The objectives of this lecture are:
ArrayLists 22-Feb-19.
Collections Framework
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
ArrayList.
TCSS 143, Autumn 2004 Lecture Notes
Part of the Collections Framework
Java Generics & Iterators
Presentation transcript:

CSC 205 – Java Programming II Lecture 25 March 8, 2002

List

The List Interface List represents an ordered collection (also known as a sequence). The List Interface implements the Collec- tion interface. The user of this interface –has precise control over where in the list each element is inserted. –can access elements by their integer index (position in the list) – can search for elements in the list.

List Operations The List interface provides four methods for positional (indexed) access to list elements. // Precondition: 0 <= index < size( ). Otherwise, // IndexOutOfBoundsException will be // thrown. // Postcondition: the element at position index has // been returned. Object get (int index);

List Operations // Precondition: 0 <= index < size( ). Otherwise, // IndexOutOfBoundsException will be thrown. // Postcondition: element has replaced the element that // was at index before this call, and that // previous occupant has been returned. Object set (int index, Object element);

List Operations // Precondition: 0 <= index <= size( ). Otherwise, // IndexOutOfBoundsException will be thrown. // Postcondition: element has been inserted at position // index and every element that was at a // position >= index before this call is now at // the next higher position. void add (int index, Object element);

List Operations // Precondition: 0 <= index < size( ). Otherwise, // IndexOutOfBoundsException will be thrown. // Postcondition: the element that was at position index // in this List before this call has been // removed, every element that was in a // position > index before this call is now // at the next lower position, and the // removed element has been returned. Object remove (int index);

List Operations The List interface provides two methods to search for a specified object // Precondition: 0 <= index < size( ). Otherwise, // IndexOutOfBoundsException will be thrown. // Postcondition: if elem does occur in this List, the index // of the first occurrence of elem has been // returned. Otherwise, -1 has been returned. int indexOf (Object elem); and, similarly int lastIndexOf (Object elem);

The ListIterator Interface The List interface provides a special iterator, called a ListIterator, that allows –element insertion and replacement –bi-directional access A method is provided to obtain a list iterator that starts at a specified position in the list.  public ListIterator listIterator()ListIterator  public ListIterator listIterator(int ind)ListIterator In addition to the hasNext and next methods, this interface provides hasPrevious and previous methods also

Hierarchical Structure

The AbstractList Class Provides a skeletal implementation of the List interface –To implement an unmodifiable list, the programmer needs only to extend this class and provide implementations for  get(int index)  size() –To implement a modifiable list, override  set(int index, Object element) –If the list is of a variable-size, override  add(int index, Object element)  remove(int index)

The ArrayList Class Resizable-array implementation of the List interface. –implements all optional list operations, –permits all elements, including null Provides methods to manipulate the size of the array that is used internally to store the list Roughly equivalent to Vector

ArrayList – An Overview ArrayList : a classy array Random access of an element from its index –public Object get(int index) –public void set(int index, Object element) Insertion or removal of an element at a given index –public void add(int index, Object element) –public Object remove(int index) Automatically resizing after invoking –public void add(int index, Object element) –public boolean add(Object o)

ArrayList – Performance The following operations run in constant time –size, isEmpty, get, set, iterator, and listIterator The add operation runs in amortized constant time, –that is, adding n elements requires O(n) time. All of the other operations run in linear time –With the constant factor is low compared to that for the LinkedList implementation.

ArrayList – Capacity Each ArrayList instance has a capacity. –the size of the array used to store the elements in the list As elements are added an ArrayList, its capacity grows automatically. –before adding a large number of elements using the ensureCapacity operation.  public void ensureCapacity(int minCapacity)

ArrayList – User’s View 1. public ArrayList (int initialCapacity) 2. public ArrayList( ) 3. public ArrayList (Collection c) 4. public boolean add(Object o 5. public void add (int index, Object element) 6. public boolean addAll(Collection c) 7. public boolean addAll(int ind, Collection c) 8. public void clear( ) 9. public Object clone( ) 10. public boolean contains (Object elem) 11. public boolean containsAll (Collection c)

ArrayList – User’s View 12. public void ensureCapacity(int minCapacity) 13. public boolean equals(Object o) 14. public Object get(int index) 15. public int hashCode( ) 16. public int indexOf (Object elem) 17. public boolean isEmpty( ) 18. public Iterator iterator( ) 19. public int lastIndexOf (Object elem) 20. public ListIterator listIterator( ) 21. public ListIterator listIterator (final int index) 22. public boolean remove (Object o)

ArrayList – User’s View 23. public Object remove (int index) 24. public boolean removeAll(Collection c) 25. public boolean retainAll(Collection c) 26. public Object set(int index, Object element) 27. public int size( ) 28. public List subList(int fromInd, int toInd) 29. public Object[ ] toArray( ) 30. public Object[ ] toArray (Object[ ] a) 31. public String toString() 32. public void trimToSize( )

Group Exercise Constructs an ArrayList that holds up to n elements public ArrayList(int initialCapacity); In a loop with i going from 0 to n – 1, appends new Double(i) to the ArrayList public boolean add(Object o); Inserts new Double(1.4) at index n/3 public void add(int index, Object element);

Group Exercise  Removes the element at index 2*n/3 public Object remove (int index); Multiplies the middle element by 3.5 public Object get(int index); public Object set(int index, Object element); Data conversion myD = new Double (3.14); // double -> Double double d = myD.doubleValue( ); // Double -> double