7/30/2019 L05 - Arrays
1/20
By Eskinder Sh. (Lec5) 1
LECTURE 5ARRAYS
A way to organize data
7/30/2019 L05 - Arrays
2/20
What are Arrays?
An array is a series of compartments to storedata.
Each compartment is appropriately sized for the
particular data type the array is declared to
store.
An array can hold only one type of data!
E.g. int[] can hold only integers
char[] can hold only charactersBy Eskinder Sh. (Lec5) 2
7/30/2019 L05 - Arrays
3/20
Array Visualization
By Eskinder Sh. (Lec5) 3
primes[0]
int[] primes = new int[10];
Specifies an array ofvariables of type int
// An array of 10 integers
index values
We are creatinga new array object
The name of
the array
The array object is oftype int
and has ten elements
primes[1] primes[2] primes[3] primes[4] primes[9]
7/30/2019 L05 - Arrays
4/20
Declaring an Array Variable
Array declarations use square brackets.
datatype[] label;
For example:
int[] prices;
String[] names;
By Eskinder Sh. (Lec5) 4
7/30/2019 L05 - Arrays
5/20
Creating a New "Empty" Array
Use this syntax: new int[20]
The newkeyword creates an array of type intthat has 20 compartments
The new array can then be assigned to an arrayvariable:
int[] prices = new int[20];
When first created as above, the items in thearray are initialized to the zero value of thedatatypeint: 0 double: 0.0 String: null
By Eskinder Sh. (Lec5) 5
7/30/2019 L05 - Arrays
6/20
Array Indexes
Every compartment in an array is assigned an
integer reference.
This number is called the indexof thecompartment
Important: In Java (and most other languages), the
index starts from 0 and ends at n-1, where n is the
size of the array
By Eskinder Sh. (Lec5) 6
7/30/2019 L05 - Arrays
7/20
Accessing Array Elements
To access an item in an array, type thename of the array followed by the items
index in square brackets.
For example, the expression:
names[0]will return the first element in thenames array
By Eskinder Sh. (Lec5) 7
7/30/2019 L05 - Arrays
8/20
Filling an Array
Assign values to compartments:
prices[0] = 6.75;
prices[1] = 80.43;
prices[2] = 10.02;
By Eskinder Sh. (Lec5) 8
7/30/2019 L05 - Arrays
9/20
Constructing Arrays
To construct an array, you can declare a newempty array and then assign values to each
of the compartments:
String[] names =new String[5];
names[0] =Goshme";
names[1] =Gonete";names[2] =Dembulo";
names[3] =Degarege";
names[4] =Mazengia";
By Eskinder Sh. (Lec5) 9
7/30/2019 L05 - Arrays
10/20
Another Way to Construct Arrays
You can also specify all of the items in an array at itscreation.
Use curly brackets to surround the arrays data andseparate the values with commas:
String[] names = {Goshme,
Gonete,Dembulo, Degarege, Mazengia};
Note that all the items must be of the same type. Herethey are of type String.
Another example:
int[] powers = {0, 1, 10, 100};By Eskinder Sh. (Lec5) 10
7/30/2019 L05 - Arrays
11/20
Length of array
String[] names = {
"David", "Qian", "Emina",
"Jamal", "Ashenafi" };
int numberOfNames = names.length;System.out.println(numberOfNames);
Output: 5
Important: Arrays are always of the same
size: their lengths cannotbe changed once
they are created! By Eskinder Sh. (Lec5) 11
7/30/2019 L05 - Arrays
12/20
Example
String[] names = {
"Aisha", "Tamara", "Gikandi", "Ato","Lauri"};
for(int i = 0; i < names.length; i++){
System.out.println("Hello " + names[i] + ".");
}
Output:
Hello Aisha.
Hello Tamara.
Hello Gikandi.
Hello Ato.
Hello Lauri.By Eskinder Sh. (Lec5) 12
7/30/2019 L05 - Arrays
13/20
Modifying Array Elements
Example:names[0] = Bekele"
Now the first name in names[] has been changed
from "Aisha" to "Bekele".
So the expression names[0] now evaluates to"Bekele".
Note: The values of compartments can change, but
no new compartments may be added. By Eskinder Sh. (Lec5) 13
7/30/2019 L05 - Arrays
14/20
Example
int[] fibs = new int[10];
fibs[0] = 1;
fibs[1] = 1;for(int i = 2; i < fibs.length; i++) {
fibs[i] = fibs[i-2] + fibs[i-1];
}
After running this code, the array fibs[]
contains the first ten Fibonacci numbers:
1 1 2 3 5 8 13 21 34 55
By Eskinder Sh. (Lec5) 14
Note: array indexes can be expressions
7/30/2019 L05 - Arrays
15/20
Exercise 1
Which of the following sequences ofstatements does notcreate a new array?
a. int[] arr = new int[4];
b. int[] arr;
arr = new int[4];
c. int[] arr = { 1, 2, 3, 4};
d. int[] arr;By Eskinder Sh. (Lec5) 15just declares an array variable
7/30/2019 L05 - Arrays
16/20
Exercise 2
Given this code fragment,
int[] data = new int[10];
System.out.println(data[j]);
Which of the following is a legal value of j?
a. -1b. 0
c. 3.5
d. 10
By Eskinder Sh. (Lec5) 16
// out of range// legal value
// out of range
// out of range
7/30/2019 L05 - Arrays
17/20
Exercise 3
Which set of data would not besuitable for storing in an array?
a. the score for each of the four quarters of aFootball match
b. your name, date of birth, and score onyour physics test
c. temperature readings taken every hourthroughout a day
d. your expenses each month for an entireyear
By Eskinder Sh. (Lec5) 17
// these are different types
7/30/2019 L05 - Arrays
18/20
Exercise 4
What is the value ofc after the following codesegment?
int [] a = {1, 2, 3, 4, 5};
int [] b = {11, 12, 13};
int [] c = new int[4];
for (int j = 0; j < 3; j++) {c[j] = a[j] + b[j];
}
c = [12, 14, 16, 0]By Eskinder Sh. (Lec5) 18
7/30/2019 L05 - Arrays
19/20
2-Dimensional Arrays
The arrays we've used so far can bethought of as a single row of values.
A 2-dimensional array can be thoughtof as a grid (or matrix) of values
Each element of the 2-D array is
accessed by providing two indexes:a row indexand a column index
(A 2-D array is actually just an array of arrays)
By Eskinder Sh. (Lec5) 19
0 1
0 8 4
1 9 7
2 3 6
value at row index 2,column index 0 is 3
7/30/2019 L05 - Arrays
20/20
2-D Array Example
Example:A landscape grid of a 20 x 55 acre piece of land:We want to store the height of the land at eachrow and each column of the grid.
We declare a 2D array two sets of square brackets:double[][] heights = new
double[20][55];
This 2D array has 20 rows and 55 columns
To access the acre at row index 11 and column index 23user: heights[11][23] By Eskinder Sh. (Lec5) 20