CH13-CollectionClasses

CH13-CollectionClass - The Art and Science of An Introduction to Computer Science ERIC S ROBERTS Jav a Collection Classes C H A P T E R 1 3 I think

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Jav a Collection Classes C H A P T E R 1 3 I think this is the most extraordinary collection of talent, of human knowledge, that has ever been gathered at the White House—with the possible exception of when Thomas Jefferson dined alone. —John F. Kennedy, dinner for Nobel laureates, 1962 13.1 The ArrayList class revisited 13.2 The HashMap class 13.3 The Java Collections Framework 13.4 Principles of object-oriented design The ArrayList Class Revisited • You have already seen the ArrayList class in section Chapter 11.8, which included examples showing how to use ArrayList s in both the pre- and post-Java 5.0 worlds. The purpose of section 13.1 is to look at the idea behind the ArrayList class from a more general perspective that paves the way for a discussion of the Java Collection Framework. • The most obvious difference between the ArrayList class and Java’s array facility is that ArrayList is a full-fledged Java class. As such, the ArrayList class can support more sophisticated operations than arrays can. All of the operations that pertain to arrays must be built into the language; the operations that apply to the ArrayList class, by contrast, can be provided by extension. The Power of Dynamic Allocation • Much of the added power of the ArrayList class comes from the fact that ArrayList s allow you to expand the list of values dynamically. The class also contains methods that add and remove elements from any position in the list; no such operations are available for arrays. • The extra flexibility offered by the ArrayList class can reduce the complexity of programs substantially. As an example, the next few slides show two versions of the code for the readLineArray method, which reads and returns an array of lines from a reader. – The first version is the one that appears in section 12.4 and uses the ArrayList class, adding each line as it appears. – The second version uses only arrays in the implementation and therefore has to allocate space as the program reads additional lines from the reader. In this implementation, the code doubles the size of the internal array each time it runs out of space. /* * Reads all available lines from the specified reader and returns an array * containing those lines. This method closes the reader at the end of the * file. */ private String readLineArray(BufferedReader rd) { ArrayList<String> lineList = new ArrayList<String>(); try { while (true) { String line = rd.readLine(); if (line == null) break; lineList.add(line); } rd.close(); } catch (IOException ex) { throw new ErrorException(ex); } String result = new String[lineList.size()]; for (int i = 0; i < result.length; i++) { result[i] = lineList.get(i); } return result; } readLineArray ( ArrayList version) /* * Reads all available lines from the specified reader and returns an array * containing those lines. This method closes the reader at the end of the * file.* file....
View Full Document

This note was uploaded on 02/18/2010 for the course CS 106A taught by Professor Sahami,m during the Fall '08 term at Stanford.

Page1 / 35

CH13-CollectionClass - The Art and Science of An Introduction to Computer Science ERIC S ROBERTS Jav a Collection Classes C H A P T E R 1 3 I think

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online