Professional Documents
Culture Documents
Arrays: A Way To Organize Data
Arrays: A Way To Organize Data
An array can hold only one type of data! E.g. int[] can hold only integers
Array Visualization
Specifies an array of variables of type int We are creating a new array object
// An array of 10 integers
index values
primes[9]
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
Filling an Array
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)
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
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]; }
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]);
a. -1 b. 0 c. 3.5 d. 10
// // // //
16
Exercise 3
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
0 0 1 2 8 9 3
1 4 7 6
19
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:
user: heights[11][23]
20