processing data in collections chapter 13. 13 object wrappers collections can only hold objects....

12
Processing Data in Collections Chapter 13

Upload: darlene-baller

Post on 11-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Processing Data in Collections Chapter 13. 13 Object Wrappers Collections can only hold objects. Primitive types ( int, double, float, etc.) are not objects

Processing Data in Collections

Chapter 13

Page 2: Processing Data in Collections Chapter 13. 13 Object Wrappers Collections can only hold objects. Primitive types ( int, double, float, etc.) are not objects

13

Object WrappersCollections can only hold objects.

Primitive types (int, double, float,etc.) are not objects.

Primitives must be wrapped in special wrapper objects.

Wrappers exist for each of the eight primitive types.

Page 3: Processing Data in Collections Chapter 13. 13 Object Wrappers Collections can only hold objects. Primitive types ( int, double, float, etc.) are not objects

13

What is an iterator?An iterator is an object that encapsulates the ability to iterate through or visit each element in the collection.

Page 4: Processing Data in Collections Chapter 13. 13 Object Wrappers Collections can only hold objects. Primitive types ( int, double, float, etc.) are not objects

13Using the Iterator

InterfacehasNext() method determines if there are more elements in the array

next() method returns the next element and advances the iterator

Page 5: Processing Data in Collections Chapter 13. 13 Object Wrappers Collections can only hold objects. Primitive types ( int, double, float, etc.) are not objects

13

The DrawShapes ApplicationUser clicks in applet window to create one of three random shapes.

Each shape is added to a collection.

When user clicks in applet window, the collection is checked to see if a “hit” is registered on an existing shape.

When a “hit” occurs, the shape’s color will change randomly.

Page 6: Processing Data in Collections Chapter 13. 13 Object Wrappers Collections can only hold objects. Primitive types ( int, double, float, etc.) are not objects

13The Shape Inheritance

HierarchyAbstract class OurShape implements:

changeColorRandomly() to change the color of an object by randomly selecting a color

contains() to determine whether x, y coordinates of a mouse click fall within a shape’s boundaries

draw() to draw the shape at x, y coordinates specified by a mouse click

Page 7: Processing Data in Collections Chapter 13. 13 Object Wrappers Collections can only hold objects. Primitive types ( int, double, float, etc.) are not objects

13Classes Derived from

OurShapeOurRectangle

OurTriangle

OurCircle

Page 8: Processing Data in Collections Chapter 13. 13 Object Wrappers Collections can only hold objects. Primitive types ( int, double, float, etc.) are not objects

13

What is a linked list?A LinkedList is a more powerful data structure that allows for quick and easy insertion and removal of elements.

Page 9: Processing Data in Collections Chapter 13. 13 Object Wrappers Collections can only hold objects. Primitive types ( int, double, float, etc.) are not objects

13

Linked ListsLinkedLists come in two types:

Singly linked list—each node knows only the next node

Doubly linked list—each node knows both the previous and the next node

Page 10: Processing Data in Collections Chapter 13. 13 Object Wrappers Collections can only hold objects. Primitive types ( int, double, float, etc.) are not objects

13

What is a set?A set is a collection with no duplicate elements.

Programmer needs to determine what constitutes a “duplicate.”

The equals() method can be used to provide an additional definition of equality.

Page 11: Processing Data in Collections Chapter 13. 13 Object Wrappers Collections can only hold objects. Primitive types ( int, double, float, etc.) are not objects

13

HashSetBased on a hash table, a HashSet is a powerful data structure that can be used to retrieve objects quickly in a set.

Unordered collection

A hash code is used to organize objects in a HashSet

Page 12: Processing Data in Collections Chapter 13. 13 Object Wrappers Collections can only hold objects. Primitive types ( int, double, float, etc.) are not objects

13

TreeSetTreeSet provides the properties of a set in a sorted collection.

Objects can be inserted in any order but are retrieved in sorted order

Uses Comparable interface to compare objects for sorting

Primitive types and String objects are comparable

Up to programmer to implement the compareTo() method for user-defined objects