LECTURE 5 ARRAYS
A way to organize data
By Eskinder Sh. (Lec5)
What are Arrays?
An array is a series of compartments to store data.
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 characters
By Eskinder Sh. (Lec5)
Array Visualization
Specifies an array of variables of type int We are creating a new array object
int[] primes = new int[10];
// An array of 10 integers
The name of the array
The array object is of type int and has ten elements
index values
primes[9]
primes[0] primes[1] primes[2] primes[3] primes[4]
By Eskinder Sh. (Lec5)
Declaring an Array Variable
Array declarations use square brackets. datatype[] label;
For example:
int[] prices; String[] names;
By Eskinder Sh. (Lec5)
Creating a New "Empty" Array
Use this syntax: new int[20]
The new keyword creates an array of type int that has 20 compartments
The new array can then be assigned to an array variable: int[] prices = new int[20]; When first created as above, the items in the array are initialized to the zero value of the datatype int: 0 double: 0.0 String: null
By Eskinder Sh. (Lec5)
Array Indexes
Every compartment in an array is assigned an integer reference.
This number is called the index of the compartment
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)
Accessing Array Elements
To access an item in an array, type the name of the array followed by the items index in square brackets.
For example, the expression:
names[0] will return the first element in the names array
By Eskinder Sh. (Lec5)
Filling an Array
Assign values to compartments:
prices[0] = 6.75; prices[1] = 80.43; prices[2] = 10.02;
By Eskinder Sh. (Lec5)
Constructing Arrays
To construct an array, you can declare a new empty array and then assign values to each of the compartments:
String[] names[0] names[1] names[2] names[3] names[4] names = new String[5]; = Goshme"; = Gonete"; = Dembulo"; = Degarege"; = Mazengia";
By Eskinder Sh. (Lec5)
Another Way to Construct Arrays
You can also specify all of the items in an array at its creation.
Use curly brackets to surround the arrays data and
separate the values with commas:
String[] names = {Goshme, Gonete,Dembulo, Degarege, Mazengia};
Note that all the items must be of the same type. Here they are of type String. Another example:
int[] powers = {0, 1, 10, 100}; By Eskinder Sh. (Lec5)
10
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 cannot be changed once they are created!
By Eskinder Sh. (Lec5)
11
Example
String[] names = { "Aisha", "Tamara", "Gikandi", "Ato", "Lauri"}; for(int i = 0; i < names.length; i++){ System.out.println("Hello " + names[i] + "."); }
Output:
Hello Hello Hello Hello Hello Aisha. Tamara. Gikandi. Ato. Lauri.
By Eskinder Sh. (Lec5)
12
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
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]; }
Note: array indexes can be expressions
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
Exercise 1
Which of the following sequences of statements does not create 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; just declares an array variable
By Eskinder Sh. (Lec5)
15
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. -1 b. 0 c. 3.5 d. 10
// // // //
out of range legal value out of range out of range
By Eskinder Sh. (Lec5)
16
Exercise 3
Which set of data would not be suitable for storing in an array?
the score for each of the four quarters of a Football match b. your name, date of birth, and score on your physics test // these are different types c. temperature readings taken every hour throughout a day d. your expenses each month for an entire year
a.
By Eskinder Sh. (Lec5)
17
Exercise 4
What is the value of c after the following code segment?
int int int for } c = [12, 14, 16, 0]
By Eskinder Sh. (Lec5)
[] a [] b [] c (int c[j]
= = = j =
{1, 2, 3, 4, 5}; {11, 12, 13}; new int[4]; = 0; j < 3; j++) { a[j] + b[j];
18
2-Dimensional Arrays
The arrays we've used so far can be
thought of as a single row of values.
A 2-dimensional array can be thought
0 0 1 2 8 9 3
1 4 7 6
of as a grid (or matrix) of values
Each element of the 2-D array is
accessed by providing two indexes: a row index and a column index
(A 2-D array is actually just an array of arrays)
value at row index 2, column index 0 is 3
By Eskinder Sh. (Lec5)
19
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 each row 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 23
user: heights[11][23]
By Eskinder Sh. (Lec5)
20