An Introduction to Objects
Download
Report
Transcript An Introduction to Objects
ArrayLists
Unit 20
Warm-up 02/014
Happy Valentine’s Day
Download
these files from the sever
(10.7.192.81/fileshare):
NameList.java
ArrayListEx1.java
turn in ErrorCheck.java
and Statistics.java (when
finished)
Please
Students will be able to:
Understand
ADT’s and how an
array implements the List ADT.
Create objects of type ArrayList
and use methods to access, add,
and remove elements.
Students will be able to:
Use
casting when retrieving
objects from ArrayLists.
Use wrapper classes to insert
primitive data values.
Vocabulary
Abstract
Data Type
List
Cast
ArrayList
Wrapper
Array Implementation of a list
A data structure combines data
organization with methods of accessing
and manipulating the data.
For example, an array becomes a data
structure for storing a list of elements
when we provide methods to find,
insert, and remove an element.
Array Implementation of a list
An abstract description of a data
structure, with the emphasis on its
properties, functionality, and use, rather
than on a particular implementation, is
referred to as an Abstract Data Type
(ADT).
The details of implementation are
hidden.
A “List” ADT
Data Organization
Contains a number data members arranged
in a linear sequence.
Methods
Create an empty List
Append to the end of the List
Remove a specified element
Retrieve the value of a given element.
Traverse the List
A “List” ADT
Arrays have this ability but we need to
keep track of both the max capacity and
the number of elements.
Since arrays are not resizable in Java
we can use an ArrayList to help solve
problems. ArrayList has this capability
which makes it very appealing.
Why not always use an ArrayList?
1. No primitive types can be put in an
ArrayList*.
2. Runs slightly slower than arrays.
3. Since these are objects they must be
type cast when retrieved from an
ArrayList.
The ArrayList Class
Declarations:
ArrayList myArrayList;
Instantiation (Java picks cap)
ArrayList myArrayList = new ArrayList();
No need to specifiy what type of object, it is
understood that these hold any object.
The ArrayList Class
ArrayList myArrayList = new ArrayList(25);
You may give an initial capacity.
These are also indexed starting at 0.
Retrieve from 0 – (size –1).
Set from 0 – (size –1).
Insert from 0 – size. (end of list)
Look at Arraylist Handout and ArrayList Demo.
The ArrayList Class
You may use this list on the AP exam.
Note that insert() and remove() adjusts the
elements.
Where as set() replaces the element.
Object Casts
Since ArrayLists can hold elements of type
object, they return items of type object and not
necessarily the type of the actual item.
Look at the code Namelist.java. Had we not type
cast the value inserted in lastOne an error
would have been thrown.
This is a type conformance issue. An object was
returned and a String object was needed.
Those Poor Primitives:
Wrappers to the rescue.
We can store ints, floats booleans if we wrap
them up first.
The classes Integer, Double, and Boolean wrap
their cohorts into an object.
Example:
Double r = new Double(8.2057);
We wrapped the double in an object, r.
Those Poor Primitives:
How do we unwrap ‘em?
Like so:
double d = r.doubleValue();
Do not forget to wrap ‘em before they go
into an ArrayList and unwrap when they
come out of an ArrayList.
ArrayList Examples
As
these examples show,
ArrayList can be very useful.
The package java.util also
includes a few other classes for
working with objects. We'll look at
some of them in later lessons.
In conclusion……
an array, an ArrayList
contains elements that are
accessed using an integer index.
However, unlike an array, the size
of an ArrayList will expand if
needed as items are added to it.
Like
Warm-up 2/22
Complete
#1-3
AP sample Questions
Warm-up 2/23
Complete
AP sample Questions
#4-7 (Skip 6 for now)
4U2Do 2/24
Tomorrow, we start Unit 21 2D Arrays
So, today be sure you have completed turned in
these dittos:
–
–
–
–
ArrayOpps
ArrayListPract
ArrayListOpps
Check for any missing work as well.
Lab 18.1 STATISTICS is due Thursday, March 3,
2005
Lab 19.1 POLYGONS is due Monday, March 7, 2005
Test – Friday, March 4, 2005 covering Units 17-20.
Exceptions, Boolean Algebra, Arrays, and
ArrayLists
Next Lab
Irregular
Polygon
You may use the built in
Point2D class.