Chapter 6
Section 6.1
- an array is a list of
values, each stored at a specific numbered position in the array
- the numbered position
is the array index or subscript
- indexes always begin at
zero
- to access value in
array, use array name followed by index in [ ]
- if it’s an array of
integers, each value in it is just an integer, so can be used anywhere a
normal integer is used
- array indexes are just
integers too, so can use integer expressions to calculate them as well
- arrays are objects – to
create an array the reference must be declared
- then array can be
instantiated using new to allocate memory space
- ex. int[] height = new
int[11];
- type is int[] – doesn’t
include size
- once array is declared
a certain size, size can’t be changed
- listing 6.1
- [] are an operator –
have highest precedence of all operators
- performs automatic
bounds checking – ensures index is in range of array size
- index must be >= 0
and < size of array – otherwise ArrayIndexOutOfBoundsException is
thrown
- array size is held in
constant called length – public constant that can be referenced
- listing 6.2, 6.3
- can declare arrays as
int[] grade or int grade[] – book uses first for consistency
- initializer list lists
initial values of array on declaration – used instead of new
- listing 6.4
- entire array can be
passed as parameter – it is an object, so copy of reference to original
array is passed
- so method can change
element of array permanently, just not the reference itself
- can pass just an
element of an array to a method, too
Section 6.2
- arrays can have references
to objects as elements
- ex. String[] words =
new String[25];
- this doesn’t create any
String objects, just an array to hold 25 of them
- listing 6.5
- parameter to main is
always an array of String objects
- main is invoked when
application is submitted to an interpreter – String[] parameter args
represents any command-line arguments that might be provided
- listing 6.6
- creation of array and
creation of objects in array are very different
- listing 6.7, 6.8. 6.9
Section 6.3
- sorting is process of
arranging a list into some order
- selection sort sorts by
repeatedly putting a value in the list into its final sorted position –
uses swapping
- listing 6.10, 6.11
- insertion sort sorts by
repeatedly inserting a value into a subset of sorted values
- listing 6.12, 6.13
- compare sorting
algorithms for efficiency – considered less efficient if it performs more
compares
- both have an outer and
inner loop , so perform n-squared comparisons for n values in the list
(order n-squared)
Section 6.4
- two-dimensional arrays
have values in two dimensions
- use two indexes, one
for rows and one for columns
- declare as int[][] - an array of arrays
- can also initialize
with initializer list
- listing 6.14, 6.15
- arrays can be
multi-dimensional too
Section 6.5
- Vector class works like
arrays and can store a list of values and reference them by an index
- it can dynamically grow
and shrink
- isn’t declared to store
a particular type – manages list of references to Objects
- listing 6.16
- indexes adjust
accordingly when elements are inserted or deleted
- objects in a Vector
object can be of different reference types
- implemented using
arrays
- elements can be added
without allocating more memory until it reaches capacity, then capacity is
expanded
Section 6.6
- polygon is multisided
shape – defined with a series of x,y points for endpoints of lines in
polygon
- polygon is always
closed
- polylines don’t have to
be closed, so can’t be filled
- listing 6.17
- Polygon class
- to save the state of a
drawing and not clear and redraw every repaint call, keep track of all
necessary information in an array or vector
- listing 6.18