Transcript Slide 1

Arrays
Chapter 8
What if we need to store test scores for
all students in our class.
We could store each test score as a
unique variable:
int
int
int
int
int
score1
score2
score3
score4
score5
=
=
=
=
=
85;
92;
90;
74;
83;
Or we could store the values in an
array
Test scores
85
92
90
74
83

An array is a data structure used to process
a collection of data that is all of the same
type
An array behaves like a numbered list of
variables with a uniform naming mechanism
 It has a part that does not change: the name of
the array
 It has a part that can change: an integer in
square brackets
 For example, given five scores:

score[0], score[1], score[2], score[3], score[4]

An array that behaves like this collection of
variables, all of type integer, can be created
using one statement as follows:
int[] score = new int[5];

Or using two statements:
int[] score;
score = new int[5];


The first statement declares the variable score to be of the array
type int[]
The second statement creates an array with five numbered
variables of type int and makes the variable score a name for the
array

The individual variables that together make
up the array are called indexed variables
They can also be called subscripted variables or
elements of the array
 The number in square brackets is called an
index or subscript
 In Java, indices must be numbered starting with
0, and nothing else

score[0], score[1], score[2], score[3], score[4]



The number of indexed variables in an array
is called the length or size of the array
When an array is created, the length of the
array is given in square brackets after the
array type
The indexed variables are then numbered
starting with 0, and ending with the integer
that is one less than the length of the array
score[0], score[1], score[2], score[3], score[4]

Test scores array
score
indices
85
92
90
74
83
0
1
2
3
4

An array can be initialized when it is declared


Values for the indexed variables are enclosed in
braces, and separated by commas
The array size is automatically set to the number of
values in the braces
int[] score = {85, 92, 90, 74, 83};

Using this shorthand notation, you have to declare,
create, and initialize the array all in one statement. Using
the two statement method would cause a syntax error

Another way of initializing an array is by using a
for loop:
int[] score = new int[5];
int index;
for (index = 0;
index < score.length; index++)
score[index] = 85;

If the elements of an array are not initialized
explicitly, they will automatically be initialized to
the default value for their base type

What is created with the following code?
public class Test {
public static void main(String[ ] args) {
int[ ] values = new int[5];
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1];
}
values[0] = values[1] + values[4];
}
}
Declare array variable values, create an
array, and assign its reference to values
public class Test {
public static void main(String[ ] args) {
int[ ] values = new int[5];
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1];
}
values[0] = values[1] + values[4];
}
} i = 1: value[1] is 1 + value[0] = 1 + 0 = 1
i = 2:
i = 3:
i = 4:
value[2] is 2 + value[1] = 2 + 1 = 3
value[3] is 3 + value[2] = 3 + 3 = 6
value[4] is 4 + value[3] = 4 + 6 = 10
value[0]
11
0
value[1]
0
1
value[2]
0
3
value[3]
0
6
value[4]
10
0
value[0] is value[1] + value[4] = 1 + 10 = 11

How do we input arrays through a scanner?
Scanner input = new Scanner(System.in);
System.out.print("Enter 10 values: ");
for (int i = 0; i < 10; i++)
myList[i] = input.nextDouble();


Strings can be part of an array
Create array of Strings with 3 elements
String[] deptName = {"Accounting",
"Human Resources", "Sales"};
for(int a = 0; a < deptName.length; ++a)
System.out.println(deptName[a]);

To print the entire array, create a loop to
print each element
for (int i = 0; i < myList.length; i++) {
System.out.print(myList[i] + " ");
}
double total = 0;
for (int i = 0; i <
myList.length; i++) {
total = total + myList[i];
}
double max = myList[0];
for (int i = 1; i < myList.length;
i++) {
if (myList[i] > max)
max = myList[i];
}

To search for a specific value, simply loop through
the loop

Look at each entry and compare it with the specified
value
boolean found = false;
int i = 0;
while (i < myList.length && !found) {
if (searchEntry == myList[i])
found = true;
i++;
}
if (found)
System.out.println(searchEntry + " occurs in array");

Both array indexed variables and entire
arrays can be used as arguments to methods

An indexed variable can be an argument to a
method in exactly the same way that any
variable of the array base type can be an
argument
Java uses pass by value to pass arguments to a method. There are
important differences between passing a value of variables of
primitive data types and passing arrays.


For a parameter of a primitive type value, the actual value is
passed. Changing the value of the local parameter inside the
method does not affect the value of the variable outside the
method.
For a parameter of an array type, the value of the parameter
contains a reference to an array; this reference is passed to
the method. Any changes to the array that occur inside the
method body will affect the original array that was passed as
the argument.
public class Test {
public static void main(String[] args) {
int[] scores = {85, 92, 90, 74, 83};
myMethod(scores);
System.out.println("scores[0] is " + scores[0]);
}
public static void myMethod(int[] numbers) {
numbers[0] = 5555; // Assign a new value to numbers[0]
}
}

Here is a method that returns the array in
reverse order
public static int[ ] reverse(int[ ] list) {
int[ ] result = new int[list.length];
for (int i = 0, j = result.length - 1; i < list.length; i++, j--) {
result[j] = list[i];
}
return result;
}