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.

Slides:



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

Chapter 24 Lists, Stacks, and Queues
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.
COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection.
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Arrays and ArrayLists Ananda Gunawardena. Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages.
Data Structures: A Pseudocode Approach with C
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L11 (Chapter 20) Lists, Stacks,
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
1 Java intro Part 3. 2 Arrays in Java Store fixed number of values of a given type Arrays are objects –have attributes –must be constructed Array declaration:
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
Datalogi A 8: 27/10. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[] data.
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.
Alice in Action with Java
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
1 More on Arrays Arrays of objects Command line arguments The ArrayList class Javadoc Review Lecture 8 notes and L&L 7.1 – 7.2 Reading for this lecture:
List Interface CS java.util.List Interface and its Implementers CS340 2.
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 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.
ArrayList, Multidimensional Arrays
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 11 Arrays Continued
© 2007 Lawrenceville Press Slide 1 Chapter 10 Arrays  Can store many of the same kind of data together  Allows a collection of related values to be stored.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
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.
Wrapper Classes and ArrayList Mrs. C. Furman 9/15/2008.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
Hashing as a Dictionary Implementation Chapter 19.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
The ArrayList Data Structure Standard Arrays at High Speed! More Safety, More Efficient, and Less Overhead!
Chapter Objectives  The List interface  Implement lists based on arrays  Learn about List applications CS340 1.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSE 1201 Object Oriented Programming ArrayList 1.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
More on Arrays Review of Arrays of ints, doubles, chars
Sets and Maps Chapter 9.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
COMP 121 Week 9: ArrayList.
Chapter 17 Object-Oriented Data Structures
Chapter 10: An Array Instance Variable
Can store many of the same kind of data together
ArrayLists.
Can store many of the same kind of data together
Object Oriented Programming in java
Lists and the Collection Interface
Code Refresher Test #1 Topics:
ArrayLists 22-Feb-19.
Can store many of the same kind of data together
Sets and Maps Chapter 9.
Presentation transcript:

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. An array is an indexed structure: can select its elements in arbitrary order using a subscript value Elements may be accessed in sequence using a loop that increments the subscript You cannot Increase or decrease the length Add an element at a specified position without shifting the other elements to make room Remove an element at a specified position without shifting other elements to fill in the resulting gap

3 The List Interface Some of the allowed operations on the List interface (java.util.List) include: Finding a specified target Adding an element to either end or in the middle Removing an item from either end or from the middle Traversing the list structure without a subscript Not all classes implementing the List interface perform the allowed operations with the same degree of efficiency

4 The List Interface and ArrayList Class

5 The ArrayList Class Simplest class that implements the List interface Improvement over an array object expandable! (ans shrinkable!) Mostly 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 How to construct and ArrayList? ArrayList myList = new ArrayList(); //each element is an Object

6 The ArrayList Class How to construct and ArrayList? (Java 5.0) ArrayList myList = new ArrayList (); //each element is a String

7 The ArrayList Class size int s = myList.size(); //returns 4 for the ArrayList below

8 The ArrayList Class Add at a specified index myList.add(2, “Doc”); Add at the end myList.add(“Dopey”);

9 The ArrayList Class Illegal if index > size() myList.add(8, “Doc”);

10 The ArrayList Class (continued) Remove at a certain index myList.remove(1);

11 The ArrayList Class (continued) cannot index like an array myList[i] ! //illegal use myList.get(i); myList.set(2, “Sneezy”); myList.set(6, “Sneezy”); //illegal if index > size()-1

12 The ArrayList Class (continued) Search using myList.indexOf(“Sneezy”); // returns 2 myList.indexOf(“Jumpy”); // returns -1

13 Specification of the ArrayList Class

14 Application of ArrayList The ArrayList gives you additional capability beyond what an array provides For Java ArrayList stores items of type Object and can thus store an object of any class You cannot store values of the primitive types directly but must instead use wrapper classes Example?? When an object is stored in an ArrayList, the programmer must remember the original type String entry = (String) myList.get(1);

15 Application of ArrayList For Java 5.0 and higher: You cannot store values of the primitive types directly but must instead use wrapper classes. However, autoboxing/autounboxing are in effect ArrayList B = new ArrayList (); B.add(42); //autoboxing int temp = B.get(0); //autounboxing No casting required when retrieving from the ArrayList ArrayList C = new ArrayList (); C.add(“Hello”); String entry = myList.get(0);

16 Recall PhoneDirectory? Instance variable theDirectory private DirectoryEntry [ ] theDirectory = new DirectoryEntry[capacity]; ArrayBased addOrChangeEntry method public String addOrChangeEntry(String name, String number) { String oldNumber = null; int index = find(name); if (index > -1) { oldNumber = theDirectory[index].getNumber(); theDirectory[index].setNumber(number); } else { add(name, number); } modified = true; return oldNumber; }

17 Using an ArrayList in PhoneDirectory Instance variable theDirectory private ArrayList theDirectory = new ArrayList (); addOrChangeEntry method public String addOrChangeEntry(String name, String number) { String oldNumber = null; int index = theDirectory.indexOf(new DirectoryEntry(name, “”)); //assuming DirectoryEntry class has an equals method that compares entries by name if (index != -1) { DirectoryEntry de = theDirectory.get(index); oldNumber = de.getNumber(); de.setNumber(number); } else { theDirectory.add(new DirectoryEntry(name, number)); } modified = true; return oldNumber; }

18 Assignment #1 (Due Thursday, Sep 24) Design and implement the Programming Project #2 described at the end of Chapter 1 (page 56), using an ArrayList as the underlying storage. Provide UML diagrams for your design. Document your code well. Place all your file under a directory called YourNameHW1 and zip the directory. a single zipped file to Submit a hardcopy of your code together with your UML diagram in class on the due date.

19 Implementation of an ArrayList Class KWArrayList: simple implementation of a ArrayList class Physical size of array indicated by data field capacity Number of data items indicated by the data field size

20 Implementation of an ArrayList Class (continued)

21 Performance of KWArrayList and the Vector Class Set and get methods execute in constant time Inserting or removing elements is linear time Initial release of Java API contained the Vector class which has similar functionality to the ArrayList Both contain the same methods New applications should use ArrayList rather than Vector