COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
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.
ITEC200 Week04 Lists and the Collection Interface.
JAVA Programming (Session 7) When you are willing to make sacrifices for a great cause, you will never be alone. Instructor:
John Hurley Cal State LA
1 Lists A List ADT Types of Lists Lists in Java Collections API Using ordered lists – Tournament Maker Using indexed lists – Josephus Problem Implementing.
The List ADT Textbook Sections
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++)
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Exception Handling – illustrated by Java mMIC-SFT November 2003 Anders P. Ravn Aalborg University.
COMP 121 Week 11: Linked Lists. Objectives Understand how single-, double-, and circular-linked list data structures are implemented Understand the LinkedList.
1 Lists A List ADT Types of Lists Lists in Java Collections API Using ordered lists – Tournament Maker Using indexed lists – Josephus Problem Implementing.
Collections Sets - no duplicates Lists - duplicates allowed Maps - key / value pairs A collection is an Object which contains other Objects. There are.
1 L41 Collections (1). 2 OBJECTIVES  What collections are.  To use class Arrays for array manipulations.  To use the collections framework (prepackaged.
Java Collections. Lecture Objectives To understand the concepts of Java collections To be able to implement Java programs based on Collections.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Cmp Sci 187: Midterm Review Based on Lecture Notes.
The Java Collections Package C. DeJong Fall 2001.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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.
Lists and the Collection Interface Review inheritance & collections.
GENERICS. Generics  Classes and methods can have a type parameter (actually, more than one).  The type parameter can be any reference (class) type.
Chapter 18 Java Collections Framework
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.
Lists and the Collection Interface Chapter 4. 2 The List Interface and ArrayList Class So far, all we have is an array for storing a collection of elements.
JAVA COLLECTIONS M. TAIMOOR KHAN (ADAPTED FROM SWINBURNE NOTES)
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Chapter 2 Collections. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Define the concept and terminology related.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
COMP 121 Week 11: Linked Lists.
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
COMP 121 Week 8: Generic Collections. Objectives To understand type variables and how they are used in generic programming To be able to implement and.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
List Interface and Linked List Mrs. Furman March 25, 2010.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Collections Using Generics in Collections. 2 Chapter Objectives Define the concept and terminology related to collections Explore the basic structure.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
 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
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
COMP 121 Week 9: ArrayList.
Object Oriented Programming in java
Collections Not in our text.
Lists and the Collection Interface
Collections Framework
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Arrays and ArrayLists.
Presentation transcript:

COMP 121 Week 9: AbstractList and ArrayList

Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection implementation into AbstractList and ArrayList implementations and justify design decisions Analyze the ArrayList implementation to determine algorithmic efficiency Use ArrayList to solve a problem

List Interface A List is an expandable collection of elements in which each element has a position or index Some lists allow random access Other lists allow sequential access Allowed operations on the List interface include: Finding a specified target Finding a specified target Adding an element to either end Adding an element to either end Removing an item from either end Removing an item from either end Traversing the list structure without a subscript Traversing the list structure without a subscript Not all classes perform the allowed operations with the same degree of efficiency List classes provide the ability to store references to Objects

Our List Interface Methods public interface List extends Collection public interface List extends Collection { void add(int index, E element); void add(int index, E element); boolean addAll(int index, Collection coll); boolean addAll(int index, Collection coll); E get (int index); E get (int index); int indexOf(Object obj); int indexOf(Object obj); int lastIndexOf(Object obj); int lastIndexOf(Object obj); ListIterator listIterator(); ListIterator listIterator(); ListIterator listIterator(int index); ListIterator listIterator(int index); E remove(int index); E remove(int index); E set(int index, E element); E set(int index, E element); List subList(int fromIndex, int toIndex); List subList(int fromIndex, int toIndex);}

List Class Hierarchy

ArrayList Simplest class that implements the List interface Improvement over an array (fixed-size data structure) Used when a programmer wants to add new elements to the end of a list, but still needs the capability to access the elements stored in the list in arbitrary order The size of an ArrayList automatically increases as new elements are added The size method returns the current size The size method returns the current size The capacity of an ArrayList is the number of elements an ArrayList can store (grows automatically!)

ArrayList (contd) The add method can: Add an element at the end of the ArrayList Add an element at the end of the ArrayListmyList.add(Bashful);myList.add(Awful);myList.add(Jumpy);myList.add(Happy); Add an element at a specific subscript position Add an element at a specific subscript positionmyList.add(2,Doc); Subsequent calls to the add method will add at the end of the ArrayList Subsequent calls to the add method will add at the end of the ArrayListmyList.add(Dopey);

ArrayList (contd)

The remove method can: Remove an element at a certain subscript position: Remove an element at a certain subscript position:myList.remove(1); You cannot access an ArrayList element directly using a subscript (like with the array), but you can use the get/set methods String dwarf = myList.get(2); myList.set(2,Sneezy); You can search an ArrayList for an element myList.indexOf(Sneezy); Will return a -1 if the element is not found Will return a -1 if the element is not found

ArrayList (contd)

Iterator Interface Defined as part of java.util package List interface declares the method called iterator, which returns an Iterator object that will iterate over the elements of that list An Iterator does not refer to or point to a particular node at any given time but points between nodes

Iterator Interface (contd)

Iterable Interface This interface requires only that a class that implements it provide an iterator() method The Collection interface extends the Iterable interface, so all classes that implement the List interface (a subinterface of Collection ) must provide an iterator() method

The Enhanced for Statement

Summary The List interface defines an expandable collection of elements in which each element has a position or index Elements in a List can be accessed using an index The Java API provides the ArrayList class, which uses an array as the underlying structure to implement the List

Summary (contd) An Iterator provides the ability to access the items in a List or Collection sequentially The Iterator interface defines the methods available to an Iterator The Iterable interface is extended by the Collection interface at the root of the Collection hierarchy The enhanced for statement makes it easy to iterate through a collection (or an array) without explicitly using an Iterator

Questions?

Exam Review – Module 5 Read and write text files Explain the purpose and use of exception handling for error detection and correction Differentiate between checked and unchecked exceptions Use the keywords throws, try, throw, catch, and finally to implement exception handling Define and use a domain-specific exception hierarchy

Exam Review – Module 6 Read and write binary files Read and write serialized object files Distinguish between random and sequential access files

Exam Review – Module 7 Compare and contrast iterative versus sequential software lifecycle approaches Use CRC cards to capture the responsibilities of each class and the relationships between classes Use UML class diagrams to illustrate relationships between classes Use primitive operation counts and deductive reasoning to determine the efficiency of algorithms Given a set of initial conditions, predict how the runtime of an algorithm is affected by increased input size

Exam Review – Module 8 Describe how generics increase code reuse and type-safety List the common operations and properties of all collections Implement the Collection interface in an AbstractCollection and ArrayCollection and justify design decisions Recognize and apply the Iterator design pattern to solve a given problem