Java Reference
In-Depth Information
and then to declare an array whose base type is this class:
Item[] items= new Item[1000];
Using a single array of a class type instead of parallel arrays will generally
simplify program structure, shorten the code that processes the data, and make
later modifications (such as adding a new property of items ) easier.
8.7
Key concepts
• Arrays . An array a0 is an object that is a list of elements called subscripted
variables.
• Array variables . An array variable b declared as
type [] b
( type is any type, e.g. int or JFrame ), initially contains null . An assignment
b= new type [n];
creates an array object with n elements and assigns its name to b .
• Subscripted variables . When b contains (the name of) an array, it has
b.length elements, or subscripted variables, which are named b[0] , b[1] , …,
b[b.length - 1] . In a subscripted variable b[i] , integer i is called the subscript
or index ; i must be in the range 0..b.length-1 .
• Array initializers . The new-expression new type [] {x 0 , x 1 , …, x n-1 } creates
an array of size n whose elements initially contain the values of expressions x 0 ,
x 1 , …, x n-1 .When used in an initializing declaration (only), this new-expression
can be abbreviated as {x 0 , x 1 , …, x n-1 } .
• Array notations . We use non-Java notation like b[h..k] to denote the segment
of array b consisting of elements b[h] , b[h + 1] , ..., b[k] . Pictures are also used.
• Tables . Sometimes, an array is used to maintain a table of values whose size is
less than the size of the array. In this case, be sure to explain in a comment near
the declaration of the array where in the array the table of values is stored.
• Array schemas . Simple schemas exist for processing all elements of an array
(or a table stored in an array). Their use can reduce programming time.
• Parallel arrays . Instead of arrays p1[0..n] and p2[0..n] , use a single array
p[0..n] , where p[i] is an object that contains the values p1[i] and p2[i] .
• Basic algorithms . There are several basic array algorithms that every pro-
grammer should be able to develop. Learn such an algorithm not by memorizing
code but by memorizing its spec and then practicing developing it from its spec.
Among these algorithms are: linear search, finding a minimum, binary search,
and selection sort.
 
Search WWH ::




Custom Search