Beruflich Dokumente
Kultur Dokumente
Array
Review
Ordered
sequence
of
elements
Array cloning
MulUdimensional
Arrays
MulUdimension
array
is
simply
an
array
of
arrays
Fill
out
dimensions
le`
to
right.
int[][] marray = new int[5][];!
for(int i=0; i<5; i++) {!
marray[i] = new int[10];!
}!
If
each
sub-dimension
is
same
size,
can
create
with
a
single
new
statement
int[][]
marray
=
new
int[5][10];
binarySearch
sort
lling
and
copying
subranges
hnp://docs.oracle.com/javase/7/docs/api/java/uUl/Arrays.html
O`en
need
a
collecUon
of
items
that
acts
like
an
array
that
can
grow
or
shrink
In
package
java.uUl
Denes
a
set
of
interfaces
for
resizeable
collecUons
List
Set
Map
List<E>
boolean
add(E
val)
E get(int index)
E remove(int index)
int size()
boolean isEmpty()
E[] toArray(E[] a)
ArrayList<E>
ArrayList<E>
is
an
implementaUon
of
List<E>
Uses
an
array
internally
lec07.ex1
Design
SituaUon
Suppose
we
have
an
object
that
encapsulates
some
sort
of
collecUon.
SongLibrary
A
collecUon
of
songs
in
an
iTunes-like
system
PolygonModel
A
collecUon
of
polygons
in
a
3D
modeling
system
Design
SituaUon
Now
suppose
we
have
code
outside
of
this
collecUon
object
that
needs
to
examine
each
element
of
the
underlying
collecUon
in
turn.
SongFilter
A
object
that
represents
a
search
criteria
to
be
applied
to
a
collecUon
of
songs
PolygonModel
public
Polygon[]
getPolygons()
Drawbacks?
May
have
to
do
a
lot
of
work
to
create
the
array
CollecUon
may
be
result
of
generaUve
process
There
may
be
no
end
to
the
collecUon.
Or
the
collecUon
may
be
large
so
we
dont
want
to
provide
the
whole
thing
at
once.
PolygonModel
Drawbacks?
Drawbacks?
Cant
have
two
traversals
going
at
the
same
Ume.
But,
this
does
come
close.
Consider:
for(int i=0; i<slist.size(); i++) {!
Song next_song = slist.get(i);!
// Do something with next_song.!
}!
Iterable<E>
Interator<E>
iterator()
Iterator<E>
Iterator<E>
boolean
hasNext()
Are
we
at
the
end
of
the
traversal?
E
next()
Get
the
next
item
of
the
traversal.
Throws
a
runUme
excepUon
if
no
next
item.
void
remove()
Not
supported
by
all
implementaUons.
Safely
removes
last
item
retrieved
by
next()
from
the
underlying
collecUon.
Iterable
examples
lec07.ex2
Main1
Simple
use
Main2
Parallel
iterators
Main3
Simultaneous
iterators
Main4
for
each
syntacUc
sugar
Dig Me Out
Jenny
Linle Babies
ArrayList<Song> slist
list
next_idx
Dig Me Out
Jenny
Linle Babies
ArrayList<Song> slist
list
next_idx
Dig Me Out
Jenny
Linle Babies
ArrayList<Song> slist
list
next_idx
Dig Me Out
Jenny
Linle Babies
lec07.ex2.Main2
Parallel
iteraUon
Processes
two
dierent
lists
Iterator
associated
with
each.
Iterators
advance
unevenly
lec07.ex2.Main3
Simultaneous
iteraUon
2
Iterators,
1
List
Insert
your
own
joke
here.
for
-
each
Java
provides
syntacUc
sugar
for
a
parUcularly
common
use
of
iterators.
for-each
loop
Supposing
e_coll
is
Iterable<E>,
then
these
are
equivalent:
Iterator<E> iter = e_coll.iterator();!
while (iter.hasNext()) {!
!E elem = iter.next();!
!// Do something with element!
}!
!
lec07.ex2.Main4
}!
lec07.ex3
A
more
complicated
iterator
Can
build
iterators
that
do
things
other
than
just
go
through
every
item.
Prior
examples
made
use
of
Iterator<E>
built
into
List<E>,
here
we
are
going
to
implement
our
own
specialized
version
of
Iterator<E>