lecture14.ppt

advertisement
CS110- Lecture 14
April 6, 2005

Agenda



Array Elements
Declaring and Using Arrays
Arrays of Objects
6/30/2016
CS110-Spring 2005, Lecture 14
1
Array Elements



Simplest Java object for managing a
list.
When writing a program that manages
a large amount of information, such as
a list of 100 names, it is not practical to
declare separate variables for each
piece of data.
Arrays solve this problem be letting us
declare one variable that can hold
multiple, individually accessible values.
6/30/2016
CS110-Spring 2005, Lecture 14
2
Array Elements




An array is a list of values.
Each value is stored at a specific,
numbered position in the array.
The number corresponding to each
position is called an index.
array syntax uses square brackets [].
6/30/2016
CS110-Spring 2005, Lecture 14
3
Array Elements
height
index
payRate
0
69
0
1
71
1 32.66
2
70
2 22.33
3
65
3
23.5
4
54
4
65.8
5
63
5
17.5
6
61
6
28.4
7
58
8
62
9
78
Value of height[6]
25
Value of payRate[3]
Length of payRate Array is 7
Length of height Array is 10
6/30/2016
CS110-Spring 2005, Lecture 14
4
Array Elements



The expression height[6] refers to a
single integer stored at a particular
location.
It can be used wherever an integer
variable can be used.
The following are valid:



height[2] = 72;
int count = 4;
height[count] = 12 * 6;
int average = (height[0] + height[1])/2;
6/30/2016
CS110-Spring 2005, Lecture 14
5
Declaring and Using Arrays




In java arrays are objects (???)
That is we have to declare array
reference and create an array using
new operator.
int[] height;
height = new int[10];
int[] height = new int[10];
height
6/30/2016
CS110-Spring 2005, Lecture 14
6
Bounds checking



Whenever a reference to an array
element is made, the index must be
greater than or equal to 0 and less than
the size of the array.
So the valid indexes for height are from
0 to 9.
If the index is not valid then Exception
called ArrayIndexOutOfBoundsException
is thrown.
6/30/2016
CS110-Spring 2005, Lecture 14
7
Bounds Checking



One way to check for bounds of an
array is to use the length constant,
which is held in the array object and
stores the size of the array.
For example: height.length contains the
value 10.
Thus the range of index is from 0 to
height.length – 1.
6/30/2016
CS110-Spring 2005, Lecture 14
8
Array Summary






declare: Type[ ] myArray;
// Type primitive or Class name
create: myArray = new Type[ size ];
put:
myArray[ index ] = …..;
get:
Type x = myArray[ index ];
length: myArray.length;
range:
0,1,…, myArray.length-1
6/30/2016
CS110-Spring 2005, Lecture 14
9
Practice Session

Lets write a class that asks the user to
enter 10 numbers and then finds the
largest of them.
6/30/2016
CS110-Spring 2005, Lecture 14
10
Initializer lists




We can use initializer lists to instantiate
an array and provide the initial values
for the elements of the array.
int[] height = {69, 70, 71, 65, 54};
The items in the list are separated by
commas and delimited by curly braces
({}).
When an initializer is used, the new
operator is not used.
6/30/2016
CS110-Spring 2005, Lecture 14
11
Array as Parameters


The entire array can be passed as
parameter to a method.
An element of an array can be passed
to a method as well.
6/30/2016
CS110-Spring 2005, Lecture 14
12
Arrays of Objects



So far we used arrays to store primitive
types such ar integers, doubles etc.
Arrays can also store references to
objects as elements.
For E.g.:
String[] strings = new String[4];
strings[0]
strings
6/30/2016
“abc”
strings[1]
“def”
strings[2]
strings[3]
“ghi”
“jkl”
CS110-Spring 2005, Lecture 14
13
Download