Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 20

Lecture 5

Arrays

A way to organize data


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
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 object is of


the array type int
and has ten elements index values

primes[0] primes[1] primes[2] primes[3] primes[4] primes[9]


Declaring an Array Variable

 Array declarations use square brackets.


datatype[] label;

 For example:
int[] prices;
String[] names;
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
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
Accessing Array Elements

 To access an item in an array, type


the name of the array followed by
the item’s index in square brackets.

 For example, the expression:


names[0]
will return the first element in the
names array
Filling an Array

 Assign values to compartments:

prices[0] = 6.75;
prices[1] = 80.43;
prices[2] = 10.02;
Constructing Arrays

 To construct an array, you can declare a


new empty array and then assign values
to each of the compartments:

String[] names = new String[5];


names[0] = "David";
names[1] = "Qian";
names[2] = "Emina";
names[3] = "Jamal";
names[4] = "Ashenafi";
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 array’s
data and separate the values with commas:
String[] names = { "David", "Qian",
"Emina", "Jamal", "Ashenafi"};
 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};
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!
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.
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.
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
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


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 // out of range
b. 0 // legal value
c. 3.5 // out of range
d. 10 // out of range
Exercise 3
 Which set of data would not be
suitable for storing in an array?
a. the score for each of the two
seasons of English Premier League
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
Exercise 4
 What is the value of c after the following
code segment?

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]
2-Dimensional Arrays

 The arrays we've used so far can be 0 1


thought of as a single row of values.
0 8 4
 A 2-dimensional array can be thought
1 9 7
of as a grid (or matrix) of values
2 3 6
 Each element of the 2-D array is
accessed by providing two indexes: value at row index 2,
column index 0 is 3
a row index and a column index

 (A 2-D array is actually just an array of arrays)


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]

You might also like