210 likes | 370 Views
Lecture 11 ArrayList and Collections. Richard Gesick. The ArrayList Class. Arrays have a fixed size after they have been instantiated. What if we don't know how many elements we will need? For example, if we are reading values from a file returning search results. The ArrayList Class.
E N D
Lecture 11ArrayList and Collections Richard Gesick
The ArrayList Class Arrays have a fixed size after they have been instantiated. What if we don't know how many elements we will need? For example, if we are • reading values from a file • returning search results
The ArrayList Class We could create a very large array, but then we waste space for all unused elements. A better idea is to use an ArrayList, which stores elements of object references and automatically expands its size, as needed
The ArrayList Class • The ArrayList class is in the package: java.util • All ArrayList elements are object references, sowe could have an ArrayList of Auto objects, Book objects, Strings, etc. • To store primitive types in an ArrayList, use the wrapper classes (Integer, Double, Character, Boolean, etc.) • The ArrayList is a generic class. The ArrayList class has been written so that it can store object references of any type specified by the client.
Declaring an ArrayList Use this syntax: ArrayList<E> arrayListName; E is a class name that specifies the type of object references that will be stored in the ArrayList. Example: ArrayList<String> listOfStrings; ArrayList<Auto> listOfCars; ArrayList<Integer> listOfInts;
ArrayList Constructors • The capacity of an ArrayList is the total number of elements allocated to the list. • The size of an anArrayList is the number of elements that are used.
Instantiating an ArrayList This list has a capacity of 10 Astronaut references, but a size of 0. ArrayList<Astronaut> listOfAstronauts = new ArrayList<Astronaut>( ); This list has a capacity of 5 Strings, but a size of 0. ArrayList<String> listOfStrings = new ArrayList<String>( 5 );
Processing Array Lists Using a standard for loop: ClassNamecurrentObject; for ( inti = 0; i < arrayListName.size( ); i++ ) { currentObject = arrayListName.get( i ); // process currentObject }
Example: Auto currentAuto; for ( inti = 0; i < listOfAutos.size( ); i++ ) { currentAuto = listOfAutos.get( i ); // process currentAuto }
The Enhanced for Loop Simplifies processing of lists. The standard form is: for ( ClassNamecurrentObject : arrayListName ) { // process currentObject }
This enhanced for loop prints all elements of an ArrayListof Strings named list: for ( String s : list ) { System.out.println( s ); }
Using an ArrayList We want to write a program for a bookstore that allows users to search for books using keywords. We will have three classes in this program: • A Book class, with instance variables representing the title, author, and price • A BookStore class that stores Book objects in an ArrayListand provides a searchForTitle method • A BookSearchEngine class that provides the user interface and the mainmethod
Collections Framework Overview • A collection is an object that represents a group of objects (such as the classic Vector class). • A collections framework is a unified architecture for representing and manipulating collections, allowing them to be manipulated independently of the details of their representation. .
Java Collection Framework • Collections are used to store, retrieve, manipulate, and communicate aggregate data. • Typically, collections represent data items that form a natural group, such as a poker hand, a collection of cards, a mail folder, a collection of letters, or a telephone directory that maps names to phone numbers.
The primary advantages of a collections framework • Reduces programming effort by providing useful data structures and algorithms so you don't have to write them yourself. • Increases performance by providing high-performance implementations of useful data structures and algorithms. Because the various implementations of each interface are interchangeable, programs can be easily tuned by switching implementations.
The primary advantages of a collections framework • Provides interoperability between unrelated APIs by establishing a common language to pass collections back and forth. • Reduces the effort required to learn APIs by eliminating the need to learn multiple ad hoc collection APIs. • Reduces the effort required to design and implement APIs by eliminating the need to produce ad hoc collections APIs. • Fosters software reuse by providing a standard interface for collections and algorithms to manipulate them.
Java Collection Framework • Collection Interfaces - Represent different types of collections, such as sets, lists and maps. These interfaces form the basis of the framework. • General-purpose Implementations - Primary implementations of the collection interfaces. • Legacy Implementations - The collection classes from earlier releases, Vector and Hashtable, have been retrofitted to implement the collection interfaces. • Special-purpose Implementations - Implementations designed for use in special situations. These implementations display nonstandard performance characteristics, usage restrictions, or behavior.
Java Collection Framework • Wrapper Implementations - Add functionality, such as synchronization, to other implementations. • Convenience Implementations - High-performance "mini-implementations" of the collection interfaces. • Algorithms- Static methods that perform useful functions on collections, such as sorting a list. • Infrastructure - Interfaces that provide essential support for the collection interfaces. • Array Utilities - Utility functions for arrays of primitives and reference objects. Not, strictly speaking, a part of the Collections Framework, this functionality was added to the Java platform at the same time and relies on some of the same infrastructure.