©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 10 - 1 Chapter 10 Using arrays to create collections.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
ArrayLists The lazy mans array. Whats the matter here? int[] list = new int[10]; list[0] = 5; list[2] = hey; list[3] = 15; list[4] = 23;
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
COMP 121 Week 11: Linked Lists. Objectives Understand how single-, double-, and circular-linked list data structures are implemented Understand the LinkedList.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13  Inheritance and Polymorphism  Access Modifiers  Abstract.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
List Implementations That Use Arrays Chapter 5 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Polymorphism.
List Implementations That Use Arrays Chapter 5. 2 Chapter Contents Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
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.
CS180 Recitation 25th/26th October, 2007 Department of Computer Science, Purdue University.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays Lists.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 10 Arrays and Collections.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Abstract Superclasses and Abstract Methods When.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Programming with Collections Grouping & Looping - Collections and Iteration Week 7.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
Chapter 8 Traffic-Analysis Techniques. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 8-1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
Multiple Choice Solutions True/False a c b e d   T F.
The Java Collections Framework (JCF) Introduction and review 1.
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Arrays : Objectives After you have read and studied this chapter, you should be able to –Manipulate a collection of data values, using an array. –Declare.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
1/20/03A2-1 CS494 Interfaces and Collection in Java.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
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.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Thomas Kuehne.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Arrays and ArrayList Files Most of these slides.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Iterators, Iterator, and Iterable 2015-T2 Lecture 8 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Thomas Kuehne.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Marcus Frean.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
Collections Dwight Deugo Nesa Matic
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays (adapted from the publisher’s.
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.
1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Arrays and ArrayList Most of these slides are.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
ADT’s, Collections/Generics and Iterators
CSE 143 Lecture 27: Advanced List Implementation
Arrays and Collections
Object Oriented Programming in java
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Presentation transcript:

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections ArrayList class

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Collections There are many examples of programs that need many objects of a particular type –A class roster needs many Student objects –A CD object needs many songs –A photo album needs many photos We can use arrays for this –How do we deal with the fact that we don't know ahead of time how many objects there will be? –Some kinds of collections change size frequently

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter What does a Collection class look like? public class ItemCollection { private Item [] collection; private int size, capacity; public ItemCollection() {…} public void add( Item item) {…} public Item get( int index) {…} …}

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Dynamic Arrays Create the array with some standard size Item [] collection = new Item[capacity]; Use a counter variable to keep track of the number of elements which are not null; Each time you add an item, put it in the next available element and increment the counter. collection[size] = newItem; size++;

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Dynamic Arrays Before adding a new item, check to see if there is room If the array is full, make a bigger array and copy the elements from the original if (size == capacity) Item [] newArray = new Item[2*capacity]; for (int i=0; i< size; i++) newArray[i] = collection[i]; collection = newArray; capacity *= 2; } // add element as above

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter JCF Lists JCF includes two classes that support methods to maintain a collection of objects as a linear list L = (l 0, l 1, l 2,..., l N ) We can add to, remove from, and retrieve objects in a given list. A list does not have a set limit to the number of objects we can add to it.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter List Methods Here are five of the 25 list methods: Returns the number of elements in the list int size ( ) Removes the element at position idx boolean remove ( int idx ) Returns the element at position idx Object get ( int idx ) Clears this list, i.e., make the list empty void clear ( ) Adds an object o to the list boolean add ( Object o )

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Using Lists To use a list in a program, we must create an instance of one of the List classes. –ArrayList –LinkedList The ArrayList class uses an array to manage data. The LinkedList class uses a technique called linked-node representation.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Sample List Usage Here's an example of manipulating a list: import java.util.*; ArrayList friends; Person person; friends = new ArrayList ( ); person = new Person("jane", 10, 'F'); friends.add( person ); person = new Person("jack", 6, 'M'); friends.add( person ); Person p = friends.get( 1 );

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter How to process the elements of a list? Use a for loop for (int i=0; i<list.size(); i++) process( list.get(i)); Use for in for (BaseType element : list) process( element); Use an iterator

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Using an Iterator The iterator method of ArrayList returns an object you can use to loop through all the elements of the list Iterator iter = friends.iterator(); while (iter.hasNext()) System.out.println( iter.next()); The next() method returns each element in turn Person p = iter.next(); Use hasNext() to determine when all the elements of the list have been processed

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Interfaces Sometimes we have a number of classes that should have a common set of methods. All lists should have methods like add, get, size, … Interfaces provide a way to specify a set of methods that a group of classes should have in common. A Java interface defines only behavior –It includes headers for public methods (no method bodies) –It does not include any data members except public constants –No instances of a Java interface can be created

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Interfaces in Java The Java libraries include many interfaces There is a List interface that includes all the methods needed by lists Both ArrayList and LinkedList implement List –They have the same methods List list; list = new ArrayList ; list = new LinkedList ; –The code works independent of which way you instantiated list –This is an example of polymorphism

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Interfaces Template for an interface public interface { public ( ); public final constName = ; }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Using an interface Use the implements keyword header of a class that needs to have all the methods of the interface Implement (provide code for) each method in the interface public class implements { public ( ){ // code for method } // add any other methods and data that is // appropriate for the class }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Using an interface type You can use an interface type as the type for a reference (object) variable ; You have to instantiate the object using a class that implements the interface = new (); Now you can only call the methods that appear in the interface.