Ch10 - Collection Classes The Need for Flexible Size...

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

View Full Document Right Arrow Icon
OOP with Java, David J Collection Classes 1 Collection Classes The Need for Flexible Size Collections. The ArrayList Class. Collections and Object Identity. The LinkedList Class. Wrapper Classes. Iterating through a Collection.
Background image of page 1

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

View Full DocumentRight Arrow Icon
OOP with Java, David J Collection Classes 2 Flexible Size Collections In some applications, the required size for a collection may be unpredictable or variable. Cards in a hand (bounded but variable). Callers to a radio phone-in (unbounded). Passengers on a ferry (unbounded?). Arrays are not always suitable for holding such data.
Background image of page 2
OOP with Java, David J Collection Classes 3 Array Unsuitability A smaller array might arbitrarily limit the capacity. A very large array will waste capacity. There might be no guarantees that it will not overflow, anyway. We could create a new array if the old one fills up. Problem with notifying aliases.
Background image of page 3

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

View Full DocumentRight Arrow Icon
OOP with Java, David J Collection Classes 4 Custom Collection Classes Two main classes, defined in java.util . ArrayList and LinkedList . No fixed size. Expand and contract as required. No wasted space, no arbitrary limit. Can only hold objects, not primitive values. Wrapper classes solve this problem.
Background image of page 4
OOP with Java, David J Collection Classes 5 An ArrayList for Strings import java.util.*; class RadioShow { ... // Store the names of callers. private ArrayList callers = new ArrayList(); } Compare with an equivalent array declaration: private String[] callers = new String[max];
Background image of page 5

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

View Full DocumentRight Arrow Icon
OOP with Java, David J Collection Classes 6 ArrayList Methods Items in a collection `lose' their identity. Each item has an index ( 0...size()-1 ). ArrayList defines add, get, set, remove and clear methods, for instance. Existing items are automatically moved. An item's index might change. The capacity grows automatically. The trimToSize method minimizes it.
Background image of page 6
OOP with Java, David J Collection Classes 7 Collections and Object Identity Objects have both a dynamic type and a static type. An object's dynamic type is fixed. An object's static type depends on the variable used to refer to it. Collections treat everything as Object . They are generally passive, not needing to interact with the items they contain.
Background image of page 7

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

View Full DocumentRight Arrow Icon
OOP with Java, David J Collection Classes 8 Object Casts Retrieving an object usually requires a cast: // ArrayList callers = getCallers(); // Get the first caller (compilation error). String caller = callers.get(0);
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/06/2010 for the course CSC 3380 taught by Professor Suptik during the Spring '09 term at LSU.

Page1 / 33

Ch10 - Collection Classes The Need for Flexible Size...

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

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