

































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Lecture notes for computer programming 1 Faculty of Engineering and Information Technology
Typology: Exercises
1 / 41
This page cannot be seen from the preview
Don't miss anything!


































Lecture notes for computer programming 1Lecture notes for computer programming 1
Faculty of Engineering and Information Technology Faculty of Engineering and Information Technology
Prepared by: Prepared by:
IyadIyad
AlbayoukAlbayouk
What is an Array? What is an Array? •
A single array variable can reference a largecollection of data.
Arrays have three important properties:
example, temperature for the last five days, orstock prices for the last 30 days.)
same data type (for example, you can create anarray of ints or an array of floats, but youcannot mix and match ints with floats.)
Using an array variable Using an array variable
Create an array variable called
studentGrades[10] studentGrades[10]
int int
studentGrades studentGrades
[] = new int[10]; [] = new int[10];
This sets up a location in memory for 10 integers whichcan be referenced using
studentGrades studentGrades
where
is the particular
student you want to look at.
Array Naming Considerations Array Naming Considerations •
The rules for naming variables apply whenselecting array variable names
underscore characters
Follow all the good programming hintsrecommended for variable names
making up the name
with a digit
array3[5] array3[5]
Elements Elements •
Refers to the individual items represented by thearray. For example,
elements
Index (Position Number or Subscript) Index (Position Number or Subscript) •
Refers to one particular element in the array
Also known as position number or, more formally,as a
subscript
The first element in an array is represented by anindex or subscript of 0 (zero). For example,
studentGrades studentGrades
This first position is known as the
zeroth element
The second position is referred to by
studentGrades studentGrades
Array positions (cont Array positions (cont
d) d)
We can access them by specifying the array namefollowed by square brackets with the indexnumber between them. For example,
System.out.println System.out.println
("The third student ("The third student
’’
s grade is " s grade is "
+ +
studentGrades studentGrades
[ 2 ] ); [ 2 ] );
Would print only the third integer spot in the array(remember 0 is the first, 1 is the second, and 2 isthe third element’s index / subscript)
The output would look like the following:
The third student’s grade is 99
Array positions (cont Array positions (cont
d) d)
The index scheme starting at 0 may be initiallyconfusing.
This is the cause of many off-by-one errors sostudy the concept carefully
The element in the array’s first position issometimes referred to as the zeroth element.
Notice the difference between "position" and"element number"
Keyword new Keyword new •
As we will see, the keyword new is used in Javawhen you wish to create a new object.
In Java, arrays are objects.
As with all data members of objects, the elementsof the each position in a new array willautomatically be initialized to the default value forthe array’s type.
Some powerful features of arrays Some powerful features of arrays •
Can use expressions as the subscript
E.g.
if variables a = 1 and b = 2 if variables a = 1 and b = 2
studentGrades studentGrades
[ a + b ] would be the same as writing [ a + b ] would be the same as writing
studentGrades studentGrades
[ 3 ] [ 3 ]
Can use array elements in expressions
E.g.
int int
gradeTotal gradeTotal
= 0 ; = 0 ;
gradeTotal gradeTotal
= =
studentGrades studentGrades
[ 0 ] +[ 0 ] +
studentGrades studentGrades
[ 1 ] + [ 1 ] +
studentGrades studentGrades
[ 2 ] + [ 2 ] +
… …
etc etc
… …
studentGrades studentGrades
[ 9 ] ; [ 9 ] ;
Would add up all the array elements and store them in gradeTotal.
So how do we use arrays? So how do we use arrays? •
Same concepts as other variables apply
variables, Java will automatically initializearrays with default values)
setting elements’ values or using their values,similar to the use of ordinary variables
Declaring an array Declaring an array •
First you can declare an array reference variable (you mustspecify the
type
of the elements in the array) :
intint
[][]
myFirstArraymyFirstArray
;;
//declares an array //declares an array
//variable for //variable for
ints ints
Note: the line above does not allocate memory for the array (itcannot since we have not said the size of the array yet). It only setsaside enough memory to reference the array. Until we create anarray, the reference is to
null
.
Next, we create the array and “point” the reference to it:
myFirstArraymyFirstArray
= new= new
intint
[10];[10];
To create the array, we need the
number
of elements in the array so
the computer can set aside adequate memory for the array.
We can combine the declaration and allocation lines intoone line as follows:
intint
[][]
myFirstArraymyFirstArray
= new= new
intint
[10];[10];
Declaring arrays (cont) Declaring arrays (cont) •
You can use a constant to set the size of the array
final final
int int
NUM_STUDENTS_IN_CLASS = 8 NUM_STUDENTS_IN_CLASS = 8
int int
[] exams = new [] exams = new
int int
[NUM_STUDENTS_IN_CLASS ]; [NUM_STUDENTS_IN_CLASS ];
In Java, you do not need to know the size of the array atCOMPILE time. Instead you can know the size at RUNtime. For example, the following is legal:
inputString inputString
= =
JOptionPane.showInputDialog JOptionPane.showInputDialog
("How ("How
many students ?");many students ?");
int int
students = students =
Integer.parseInt Integer.parseInt
( (
inputString inputString
); );
int int
[] []
myFirstArray myFirstArray
=
new
=
new
int int
[students]; [students];
Initializing an Array Initializing an Array
You can initialize an array when you declare it, asyou do with other variables
Syntax is slightly different, as you are nowinitializing more than one element at a time
One way at declaration, using
initializers
int int
myFirstArray myFirstArray
[
] = { 0, 0, 0, 0, 0 };
[
] = { 0, 0, 0, 0, 0 };
Note the braces around the initial zeroes whichthemselves are separated by commas.
Also note that creating arrays in this way eliminatesthe need for the keyword new.