Lesson_06

advertisement
Lesson 06 - Arrays
a. basic array concepts
1.
every array is an object in Java
Arrays are composed of elements. An array element can be used like any stand-alone variable of the same
data type.
2.
declare an object reference variable (informally, the “name” of the array)
int count[]; // count will be used to access an array object created later.
3.
create an array object and assign it’s reference to an object reference variable
count = new int[10]; //reference variable exists, create array of 10 integers
Note: all elements will be initialized to 0 (default for int data type).
4.
declare an object reference variable, create an array object, and assign the reference to the object reference
variable in a single step
int count[] = new int[10];
5.
declare an object reference variable, create an array object using an initializer list, and assign the reference to
the object reference variable
int count[] = {1,2,3,4,5}; //Create a 5 element array and assign the reference to count
Note: the size of the array AND the values of the elements in the array are determined by the initializer list
6.
accessing an element in an array
array-name[ element-index ] Examples: count[0], count[1]
Valid indexes/subscripts are in the range : [0, array-size – 1] Example: count[count.length - 1]
The index/subscript is any valid integer expression (named constant, literal, integer variable, integer
expression)
7.
named constants in array declarations
Example: final int SIZE;
int x[] = new int[SIZE];
8.
length field to determine the size of an array
Example: int size = x.length; //x is an array name
length is the name of a public field in an array object. It can be used to determinal the number of elements in
any array. Syntax is array-name.length. array-name is actually the object reference variable.
9.
memory concepts
Contiguous array elements
Size in bytes and data type of each element must be the same
10.
value arrays versus reference arrays
The data type for the elements in an array is either value types (aka PDT) or a reference types.
Think of an array of references as an array of objects conceptually.
An example of an array of references (objects) would be an array of Strings. Strings are objects in Java.
String stooges[] = { “Moe”,“Larry”,“Curly” }; // stooges[0] is “Moe”, stooges[1] is “Larry”.
11.
constant arrays ( compile-time or run-time )
Java does not actually support “constant” arrays. For our purposes, we will create arrays, initialize the values
and then not make any changes to the array.
compile-time arrays are created using an initializer list
run-time arrays are created using the keyword new and then assigned values using a block of statements
or reading data from a file and storing it in the array elements.
12.
subscripts/indexes
These are the names used to refer to a specific element in an array. You may also see the term offset –
primarily if using C or C++.
13.
zero-based indexing
In Java (as in many languages), the index of the first element is 0. This is called zero-based indexing.
14.
A null reference in Java is a reference with the address 0. An address of 0 means the reference is not currently
associated with an object or has been disassociated from the object. Examples:
a. String students[] = new String[25]; // This statement creates an array of null references.
Eventually the elements would likely be assigned references to String objects. Folllowing is an example that
replaces a null reference with a reference to a String object.
Example:
students[0] = “Jack Wilson”;
// now the null reference has been replaced by a
// reference to a String object
b. stooges = null; // This statement disassociates the object reference variable stooges from the array
// object it was previously associated with
b. bounds-checking
1. using a named constant
2. using the length field
final int MAX_SIZE = 100;
a. myArray.length
b. for (int j=0; j<intArray.length; j++)
3. exceptions – ArrayIndexOutOfBoundsException
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at TestArray.main(TestArray.java:29)
c. searching an array for an exact match
This involves creating an algorithm to search the array looking for a match in one of the elements. You could use a
lecture book example and write in Java.
1. set a flag (a boolean variable) when you find a match
Boolean variables in Java may be assigned true or false (all lowercase, no quotes). A boolean variable may also
be assigned the result of a boolean expression (aka relational comparison expression).
boolean found = false;
if ( array[x] == inputVaue ) //array is not a reference array
found = true.
if ( found )
System.out.println(" <some message>
");
Download