COMP 121 Week 9: ArrayList.

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

ITEC200 Week04 Lists and the Collection Interface.
COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection.
John Hurley Cal State LA
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
COMP 121 Week 11: Linked Lists. Objectives Understand how single-, double-, and circular-linked list data structures are implemented Understand the LinkedList.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Chapter 19 Java Data Structures
COMP 121 Week 14: Queues. Objectives Learn how to represent a queue Learn how to use the methods in the Queue interface Understand how to implement the.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
COMP 121 Week 7: Object-Oriented Design and Efficiency of Algorithms.
COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
111 © 2002, Cisco Systems, Inc. All rights reserved.
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
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
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)
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
COMP 121 Week 11: Linked Lists.
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.
Chapter Objectives  The List interface  Implement lists based on arrays  Learn about List applications CS340 1.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
Sets and Maps Chapter 9.
Slides by Donald W. Smith
Using the Java Collection Libraries COMP 103 # T2
Objects First with Java CITS1001 week 4
Stacks Chapter 5.
Ch7. List and Iterator ADTs
Chapter 19 Java Data Structures
John Hurley Cal State LA
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Chapter 20 Lists, Stacks, Queues, and Priority Queues
JAVA COLLECTIONS LIBRARY
JAVA COLLECTIONS LIBRARY
ARRAYLIST AND VECTOR.
Java Collections Overview
Chapter 20 Lists, Stacks, Queues, and Priority Queues
The ArrayList Class An ArrayList is a complex data structure that allows you to add or remove objects from a list and it changes size automatically. The.
ArrayLists.
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Programming II (CS300) Chapter 07: Linked Lists and Iterators
CS2013 Lecture 4 John Hurley Cal State LA.
Can store many of the same kind of data together
Dynamic Data Structures and Generics
Object Oriented Programming in java
Collections Not in our text.
Lists and the Collection Interface
JCF Collection classes and interfaces
Sets and Maps Chapter 9.
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Presentation transcript:

COMP 121 Week 9: ArrayList

Objectives To understand the List interface To examine how the List interface is implemented by the ArrayList class To introduce the Iterator and Iterable interfaces To review the enhanced for statement

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 Adding an element to either end Removing an item from either end 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 Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

List Interface and ArrayList Class Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

ArrayList Class Simplest class that implements the List interface Improvement over an array 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 capacity of an ArrayList is the number of elements an ArrayList can store (automatically increased) Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

ArrayList Class (cont’d) The add method can: Add an element at the end of the ArrayList myList.add(“Bashful”); myList.add(“Awful”); myList.add(“Jumpy”); myList.add(“Happy”); Add an element at a certain subscript position myList.add(2,”Doc”); Subsequent calls to the add method will add at the end of the ArrayList myList.add(“Dopey”); Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

ArrayList Class (cont’d) Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

ArrayList Class (cont’d) The remove method can: Remove an element at a certain subscript position myList.remove(2); You cannot access an ArrayList element directly using a subscript, 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 Uses the equals method myList.indexOf(“Sneezy”); Will return a -1 if the element is not found Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

The ArrayList Class (cont’d) Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Specification of the ArrayList Class Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Iterator<E> Interface The Iterator interface is defined as part of API package java.util The 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 Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

The Iterator<E> Interface (cont’d) Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

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 Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

The Enhanced for Statement Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Enhanced for Statement Is another way of doing … Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

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<E> class, which uses an array as the underlying structure to implement the List Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Summary (cont’d) 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 Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Any Questions?