Arrays - Eastern Michigan University

Download Report

Transcript Arrays - Eastern Michigan University

Arrays
http://java.sun.com/
 Writing a program that uses a large
information.
amount of
 Such as a list of 100 elements.
 It is not practical to declare variables for each
piece of data.
Arrays
Arrays solve this problem
Letting us declare one variable.
This can hold multiple accessible
values.
Array Indexing
 An array is a list of values.
 Each value is stored at
position.
a specific,
 To access a value, we use the name of
the array.
Array Indexing
 The total of elements
is called the size.
 An array of size “N” is
indexed from 0 to N-1.
Declaring and using arrays
In Java, arrays are objects.
To create an array, the reference must be declared.
The array can then be created using the new
operator.
Code Example:
int [ ] height = new int [10] ;
The variable height is declared to be an array.
The creation of height, using the new operator.
[ ] Square Brackets
The square brackets used to indicate the index.
The index performs automatic bounds checking.
Bounds checking ensures that the index is in the
range.
Example:
An array called prices is created with 25 elements.
The value of the index is checked from 0 to 24.
Otherwise an exception is called.
“ArrayIndexOutOfBoundsException.”
length
The size of the array is held in a constant
called length.
After the array prices is created with 25
elements, the constant prices.length contains
the value 25.
Example:
for ( int i = 0; i < prices.length; i++ ) {
System.out.println( prices[ i ] );
}
Array syntax
First is to associate the brackets with the type of
values stored in the array:
int [ ] grades ;
Second is to associate the brackets with the name
of the array:
int grades [ ] ;
Initializing arrays
A technique for instantiating arrays is using an
initializer list, which provides the initial values for
the array.
The items in an initializer list are separated by
commas and delimited by braces. The size of the
array is determined by the number of items.
Example of initializer list
When an initializer list is used, the new operator
is not used.
This declaration instantiates the array grades as
an array of six integers, indexed from
0 to 5:
int [ ] grades = { 87, 98, 69, 99, 88, 76 }
Arrays as parameters
Because an array is an object, when an array is
passed as a parameter, a copy of the reference to
the original array is passed.
A method that receives an array as a parameter can
permanently change an element of the array
because it is referring to the original value.
Arrays of objects
Arrays store primitive types such as integers and
characters.
Arrays can also store references to objects as
elements.
An array could contain objects that consist of
several variables and the methods that use them.
Example:
for ( int port = 0; port <= 4350; port++ ) {
try {
ServerSocket server = new ServerSocket( port );
}
catch (IOException e) {
System.out.println( “There is a server on port: “
+ port + “ . “ );
Arrays of string objects
The variable words is an array of references to
String objects.
String [ ] words = new String [ 25 ] ;
The new operator in the declaration instantiates the
array and reserves space for 25 String references.
Two-dimensional arrays
Two-dimensional array has values in two
directions, which are often represented as the
rows and columns of a table.
The type of a two-dimensional array that stores
integers is:
int [ ] [ ] ;