1 / 21

Lecture 11 ArrayList and Collections

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.

brooks
Download Presentation

Lecture 11 ArrayList and Collections

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 11ArrayList and Collections Richard Gesick

  2. 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

  3. 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

  4. 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.

  5. 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;

  6. 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.

  7. 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 );

  8. ArrayList Methods

  9. More ArrayList Methods

  10. Processing Array Lists Using a standard for loop: ClassNamecurrentObject; for ( inti = 0; i < arrayListName.size( ); i++ ) { currentObject = arrayListName.get( i ); // process currentObject }

  11. Example: Auto currentAuto; for ( inti = 0; i < listOfAutos.size( ); i++ ) { currentAuto = listOfAutos.get( i ); // process currentAuto }

  12. The Enhanced for Loop Simplifies processing of lists. The standard form is: for ( ClassNamecurrentObject : arrayListName ) { // process currentObject }

  13. This enhanced for loop prints all elements of an ArrayListof Strings named list: for ( String s : list ) { System.out.println( s ); }

  14. 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

  15. 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. .

  16. 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.

  17. 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.

  18. 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.

  19. Java Collection Framework

  20. 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.

  21. 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.

More Related