Professional Documents
Culture Documents
Chapter 8:
Arrays
Starting Out with Java:
From Control Structures through Data Structures
Second Edition
by Tony Gaddis and Godfrey Muganda
Introduction to Arrays
Primitive variables are designed to hold only
one value at a time.
Arrays allow us to create a collection of like
values that are indexed.
An array can store any type of data but only
one type of data at a time.
An array is a list of data elements.
2012 Pearson Education, Inc. All rights reserved.
8-3
Creating Arrays
An array is an object so it needs an object reference.
// Step 1: Declare a reference to an array that will hold
integers.
int[] numbers;
// Step 2: Create a new array that will hold 6 integers.
index 0
index 1
index 2
index 3
index 4
index 5
8-4
8-5
numbers[0]
numbers[1]
numbers[2]
numbers[3]
numbers[4]
numbers[5]
8-6
System.out.println(Number: + numbers[i]);
}
The Enhanced for Loop
an alternative to accessing the array
for(int val : numbers)
{
System.out.println(Number: " + val);
}
Enhanced for-Loops = simplified array processing (read only)
Always goes through all elements
8-9
Bounds Checking
Array indexes always start at zero and continue to
(array length - 1).
int[] values = new int[10];
8-10
Off-by-One Errors
It is very easy to be off-by-one when accessing arrays.
// This code has an off-by-one error.
int[] numbers = new int[10];
for (int i = 1; i <= 10;
10 i++)
{numbers[i] = 0;}
Index
out of
bounds
Here, the equal sign allows the loop to continue on to index 10,
where 9 is the last index in the array.
This code would throw an
ArrayIndexOutOfBoundsException.
2012 Pearson Education, Inc. All rights reserved.
8-11
Activity
1.) Lets define an array of your top 10 favorite music artists.
2.) Create a for-loop to ask the user for each name of their fav
artist.
a.) Use the for-loop index as the index of the array.
3.) Create a separate (parallel) array of your favorite song for
each artist, in exactly the same order as the artist array.
a.) Use the for-loop again to initialize it.
4.) Print the 2 parallel arrays, formatting the output so that the
artist name is printed first, and beside it favorite song for that
artist.
2012 Pearson Education, Inc. All rights reserved.
2 re f
er e
varia nce
b
point les, are
in g to
on e a
r
mem ray in
ory.
Copying Arrays
int[] array1 = { 2, 4, 6, 8, 10 };
int[] array2 = array1; //This does not copy
2
array1 holds an
address to the array
Address
array2 holds an
address to the array
Address
arrays.
8 10
Instead,
Instead you need to copy each element of one array to another:
ay
W
t
gh
Ri !!!
8-15
5 10 15 20 25 30 35 40
Address
8-16
8-17
int[] firstArray = { 2, 4, 6, 8, 10 };
int[] secondArray = { 2, 4, 6, 8, 10 };
boolean arraysEqual = true;
int i = 0;
8-18
8-19
8-20
Activity
1.) In main, define an array of your own grades, where each element
of the array is the score you received on each of your labs and
homework assignments (define & initialize in 1 step).
2.) In the same driver class, create a static method called
calculateGradesStatistics(double[] myArray). This method will
receive your array of grades as an input parameter.
3.) In the method, use a for-loop that will determine:
a.)
b.)
c.)
d.)
e.)
8-23
8-24
Address
names[0]
address
Bill
names[1]
address
Susan
names[2]
address
Steven
names[3]
address
Jean
8-25
null
names[1]
null
names[2]
null
names[3]
null
=
=
=
=
"Bill";
"Susan";
"Steven";
"Jean";
toUpperCase
compareTo
equals
charAt
8-27
null
accounts[1]
null
accounts[2]
null
accounts[3]
null
accounts[4]
null
balance:
0.0
balance:
0.0
balance:
0.0
balance:
0.0
balance:
8-28
0.0
2.) Define a Student class, with the 4 attributes defined above. Create a .toString() method in
the Student class, that will return the first name + last name + student id + GPA.
Group 2 in Table:
3.) Define a Driver class. In the main method, read each record from students.txt, and create a
Student object from each record. Call a static method that will receive the name of the
students.txt file, and will create and return an array of Student objects.
4.) Print the array of Student objects.
5.) Copy the selectionSort() method from Lab #7 to the bottom of the Driver class. Adjust the
logic to sort by GPA.
6.) Invoke the selection sort method, passing it the array of Student objects. Print the array
of Student objects again (should be sorted).
2012 Pearson Education, Inc. All rights reserved.