Introduction To Practical Computer Science I - Lec 12 (Arrays).pptx
1. Introduction To Practical Computer Science I
Lecture 12: Arrays
Herat University
Computer Science Faculty
Lecturer: Abdul Khaleq Herawi
Date: 2024
2. 2
Outline:
Creating and initializing arrays
IndexedVariables
Array Initializers
Array Initialization using shorthand notation
3. INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 3
OPENING PROBLEM
Read one hundred numbers, compute their average, and find out how many numbers are
above the average.
4. INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 4
Introducing Arrays
Array is a data structure that represents a collection of the same types of data.
5.6
4.5
3.3
13.2
4
34.33
34
45.45
99.993
11123
double[] myList = new double[10];
myList reference
myList[0]
myList[1]
myList[2]
myList[3]
myList[4]
myList[5]
myList[6]
myList[7]
myList[8]
myList[9]
Element value
Array reference
variable
Array element at
index 5
5. INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 5
Declaring ArrayVariables
datatype[] arrayRefvar;
example:
double[] mylist;
datatype arrayRefvar[]; / / this style is allowed, but not preferred
example:
double mylist[];
6. INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 6
Creating Arrays
arrayRefvar = new datatype[arraysize];
example:
mylist = new double[10];
mylist[0] references the first element in the array.
mylist[9] references the last element in the array.
7. INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 7
DECLARING AND CREATING IN ONE STEP
datatype[] arrayrefvar = new datatype[arraysize];
double[] mylist = new double[10];
8. INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 8
The Length of an Array
Once an array is created, its size is fixed. It cannot be changed. You can find its size using
arrayRefvar.length
For example,
Mylist.length returns 10
9. INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 9
DEFAULTVALUES
When an array is created, its elements are assigned the default value of
0 for the numeric primitive data types,
'u0000' for char types, and
false for boolean types.
10. INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 10
IndexedVariables
The array elements are accessed through the index. The array indices are 0-based, i.e., It
starts from 0 to arrayrefvar.length-1.
Each element in the array is represented using the following syntax, known as an indexed variable:
arrayRefvar[index];
11. INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 11
Using IndexedVariables
After an array is created, an indexed variable can be used in the same way as a regular
variable. For example, the following code adds the value in mylist[0] and mylist[1] to
mylist[2].
mylist[2] = mylist[0] + mylist[1];
12. INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 12
Array Initializers
Declaring, creating, initializing in one step:
double[] mylist = {1.9, 2.9, 3.4, 3.5};
This shorthand syntax must be in one statement.
13. INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 13
Declaring, creating, and initializing using the shorthand notation
double[] mylist = {1.9, 2.9, 3.4, 3.5};
This shorthand notation is equivalent to the following statements:
double[] mylist = new double[4];
mylist[0] = 1.9;
mylist[1] = 2.9;
mylist[2] = 3.4;
mylist[3] = 3.5;
14. INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 14
Caution!
Using the shorthand notation, you have to declare, create, and initialize the array all in one statement. Splitting it would
cause a syntax error. For example, the following is wrong:
double[] mylist;
mylist = {1.9, 2.9, 3.4, 3.5};