Sie sind auf Seite 1von 11

UNIT 1: REVIEW

Array Review
Learning Goals
 We will learn:
 To declare and create arrays
 To process arrays to find: sum, min/max, average of
values in the list
 To write methods that return an array
 Write methods that accept an array in the
parameter list
What is an Array?
 An array is:
 An (reference) object used to store a list of values
 A contiguous block of memory is used to store the
values
 Each cell holds a value

 All the values are the same type


What can an array store?
 An array can store any one type of data:
 int
 double
 char
 boolean
 String
 Objects
 Etc.

 The type of data stored can be primitive or reference


An Array Example
 Looking at the array on the right:
 The name of the array is bowlingScores
 The type of data stored in the array is int bowlingScores
 Each cell can be accessed by its index number 0 80
 Ex: bowlingScores[3] holds the value 138
1 120
 If there are n cells, the indexes will be 0 through n-1
 Ex. 8 cells gives indexes 0 through 7 2 103
3 138
4 94
What value is in bowlingScores[6]? 83
5 109
What value is in bowlingScores[0]? 80 6 83
What value is in bowlingScores[8]? AOB Error
7 117
Declaring an Array
 General syntax for declaring an array:
type[] arrayName = new type[length]; bowlingScores
0 80
final int MAX = 8;
int[] bowlingScores = new int[MAX]; 1 120
bowlingScores[0] = 80; //initializing
bowlingScores[1] = 120; //initializing 2 103
… 3 138
 Once an array has been created, the number of cells 4 94
cannot be changed
5 109
6 83
7 117
Initializing an Array With a list of
values
 In order to declare and initialize variables at the
same time, you can use this option:
0, 1, 2, 3, 4, 5
int[] luckyNumbers = {2, 3, 10, 4, 17, 21}; // known values

 Array length is not needed when initializer list is used


 The compiler will count the value in the list and make that
many cells
 Initializer lists are usually used for small arrays
Summing Values in an Array

int sum; // declare an accumulator


sum = 0; // initialize accumulator
for (int i=0; i < MAX; i++) {
sum = sum + list[i]; // sum += list[i];
}
System.out.println(“The sum of all values in the array is” +
sum);
Summing Values in an Array

int sum; // declare an accumulator


sum = 0; // initialize accumulator
for (int i=0; i < MAX; i++) {
sum = sum + list[i]; // sum += list[i];
}
System.out.println(“The sum of all values in the array is”
+ sum);

Note: You can trace your program and track the values by using a memory map ( a
table where the variables are listed in the order they were declared and the values
stored are recorded)
Method that accepts an array as a
Parameter

public int findSum(int[] list) {


int sum; // declare an accumulator
sum = 0; // initialize accumulator
for (int i=0; i < MAX; i++) {
sum = sum + list[i];
}
return sum;
}
Method that Returns an Array

public int[] getNewList() {


int[] list = new int[10];
for (int i=0; i<10; i++) {
list[i] = i;
}

return list;
}

Das könnte Ihnen auch gefallen