(Java Collections Framework) AbstractSequentialList

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Linked Lists Linear collections.
Collections Chapter Java Collection Frameworks The Java collection framework is a set of utility classes and interfaces. Designed for working with.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
CS 106 Introduction to Computer Science I 04 / 27 / 2007 Instructor: Michael Eckmann.
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.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
12-Jul-15 Lists in Java Part of the Collections Framework.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
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.
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.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
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.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
12/18/2015ITK 2751 CollectionList A bstractSequentialList Vector Stack LinkedList {interface} AbstractList {abstract} ArrayList Java Provides a List interface.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
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
1 CS162: Introduction to Computer Science II Abstract Data Types.
9/27/2016IT 1791 Abstraction A tool (concept) to manage complexity Hide irrelevant details; focus on the features needed Primitive date types are already.
EKT472: Object Oriented Programming
The List ADT Reading: Textbook Sections 3.1 – 3.5
Linked Lists Chapter 5 (continued)
Software Development Java Collections
Abstraction A tool (concept) to manage complexity
Data Structures Lakshmish Ramaswamy.
COP 3503 FALL 2012 Shayan Javed Lecture 8
Implementing ArrayList Part 1
Introduction to Collections
TCSS 143, Autumn 2004 Lecture Notes
ArrayLists.
Programming in Java Lecture 11: ArrayList
Data Type (how to view it)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Introduction to Collections
ArrayLists.
The List ADT Reading: Textbook Sections 3.1 – 3.5
CSE 143 Lecture 27: Advanced List Implementation
ArrayList Collections.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Introduction to Collections
ArrayLists 22-Feb-19.
Collections Framework
slides created by Marty Stepp
JCF Collection classes and interfaces
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Linked Lists Chapter 5 (continued)
ArrayLists.
ArrayLists 27-Apr-19.
Programming II (CS300) Chapter 07: Linked Lists
Iterators Dan Fleck.
Chapter 6 The List ADT.
Part of the Collections Framework
Linked Lists Chapter 5 (continued)
Java Generics & Iterators
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Presentation transcript:

(Java Collections Framework) AbstractSequentialList JCF (Java Collections Framework) Collection List {interface} {interface} Java Provides a List interface for several implementations RandomAccess AbstractList {interface} {abstract} ArrayList Vector AbstractSequentialList {abstract} Stack LinkedList 11/28/2018 IT 179

ArrayList Method Summary  boolean add (E o) Appends the specified element to the end of this list.  void add (int index, E element) Inserts the specified element at the specified position in this list. clear () Removes all of the elements from this list. ensureCapacity (int minCapacity) Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.  E get (int index) Returns the element at the specified position in this list.  int indexOf (Object e) Searches for the first occurrence of the given argument, testing for equality using the equals method. isEmpty () Tests if this list has no elements. 11/28/2018 IT 179

ArrayList Method Summary remove (int index) Removes the element at the specified position in this list.  boolean remove (Object o) Removes a single instance of the specified element from this list, if it is present (optional operation). set (int index, E element) Replaces the element at the specified position in this list with the specified element.  int size () Returns the number of elements in this list.  Object[] toArray () Returns an array containing all of the elements in this list in the correct order.  void trimToSize () Trims the capacity of this ArrayList instance to be the list's current size. 11/28/2018 IT 179

Interface for the user ArrayList LinkedList Vector Stack XXXX List .... Interface for the user public int size(); public T get(int i); public T set(int i, T item); public int indexOf(T item); public void add(T item); public void add(int i, T item); public T remove(int i); public Object[] toArray(); public <K> K[] toArray(K[] a); 7 2 4 6 1 3 5 8 11/28/2018 IT 179

Interface for the user 1 [0] [1] 2 [2] 3 [3] 4 [4] 5 [5] 6 [6] 7 [7] 8 ArrayList< > 1 [0] public int size(); public T get(int i); public T set(int i, T item); public int indexOf(T item); public void add(T item); public void add(int i, T item); public T remove(int i); public Object[] toArray(); public <K> K[] toArray(K[] a); [1] 2 [2] 3 [3] 4 [4] 5 [5] 6 [6] 7 [7] 8 [8] [9] 11/28/2018 IT 179

ArrayList class (generic) import java.util.ArrayList; ..... ArrayList<String> L = new ArrayList<String>(); L.add(“Good"); L.add(“Afternoon"); L “Good” “Afternoon” 11/28/2018 IT 179

What happen behind the scene import java.util.ArrayList; ..... ArrayList<String> L = new ArrayList<String>(8); L.add(“Smith"); L.add("Robinson"); L.set(0,"Good"); … Good L L Smith Smith Robinson Size=1 Size=2 L Size=0 Capacity =8 Capacity =8 Capacity=8 11/28/2018 IT 179

L …… Good L.add(“Smith"); L.add("Robinson"); L.set(0,"Good"); L.add(1, "Mrs."); Good Smith Mrs. Robinson L !! Size=2 Size=3 Size=4 Capacity=8 11/28/2018 IT 179

L …… Good L.add(“Smith"); L.add("Robinson"); L.set(0,"Good"); L.add(1, "Mrs."); L.add(1, "Afternoon"); L.add(2, "!"); Good Afternoon Mrs. Robinson L !! Size=4 Size=5 Capacity=8 IT 179 11/28/2018

L …… Good L.add(“Smith"); L.add("Robinson"); L.set(0,"Good"); L.add(1, "Mrs."); L.add(1, "Afternoon"); L.add(2, "!"); Good Afternoon Morning ! L.set(1, "Morning"); Mrs. L Robinson !! Size=6 Capacity=8 11/28/2018 11/28/2018 IT 179

AbstractSequentialList The Java Collections Framework (JCF) Collection List {interface} {interface} Java Provides a List interface for several implementations RandomAccess Iterable AbstractList {interface} {interface} {abstract} ArrayList Vector AbstractSequentialList {abstract} Stack LinkedList 11/28/2018 IT 179

Need for Iterators O(n) The internal information 1 5 iterA 2 4 3 3 4 2 5 3 iterB 1 6 2 7 1 8 iterC while (iter.hasNext()) { ..... ..... iter.next()..... } The internal information and structures are protected. 8 6 7 4 O(n) 1 3 2 5 11/28/2018 IT 179

AbstractList<T> Iterable<T> {interface} + Iterator<T> iterator(); AbstractList<T> {interface} (see the java document) …. is-relation has-relation is-relation Iterator<T> {interface} + boolean hasNext(); + T next(); + void remove(); ArrayList<T> 11/28/2018 IT 179

API java.util.Iterator<E> Interface (usually for some internal class) Iterator<E> bool hasNext(); E next(); void remove(); import java.util.Iterator; ..... static ArrayList<Card> pick(char suit, PokerDeck deck) { ArrayList<Card> a = deck.toArrayList(); Iterator<Card> aiter = a.iterator(); while (aiter.hasNext()) { if (aiter.next().getSuit() != suit) aiter.remove(); } return a; 11/28/2018 IT 179

Ace Finding Game H2 H13 H3 H4 H5 H7 H12 H10 H6 H1 H8 H9 H11 11/28/2018 IT 179