CSI247-04 Array
CSI247-04 Array
1
LECTURE OBJECTIVES
2
Collection
Collection:
A group or set of related things(objects). Any two
objects in a collection should be comparable.
Examples
A collection of music CDs.
A collection of books.
A collection of QBoards, Strings, Accounts, etc.
3
Problem
4
Possible solution?
import java.util.∗;
public class ComputeAverage {
public static void main (String[ ] args) {
Scanner scan = new Scanner (System.in);
double mark, average, sum = 0.0;
for (int i = 1 ; i <= 1 0 ; i++){
System.out.printf("Enter lab mark");
mark = scan.nextDouble();
sum += mark;
}
average = sum/10;
// then what ?
}
5
What is wrong with the approach?
6
Solution?
Arrays
Array:
An object that allows us to store items of the
same kind (collection).
Each item/object is stored in its own cell.
Cells are numbered from 0 to array.length − 1.
It supports the following operations:
• add an item/element
• retrieve an item/element
7
1. Creating One Dimensional Arrays
8
2. Creating Arrays: Memory Model
int[ ] numbers = new int[10]; // array to hold 10 integers
numbers
48
48
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6
9
3. Creating and Initializing Arrays
Declare and Initialize:
int[] nums = {10, 6, -7, 9, 19};
char[] grades = {’A’, ’B’, ’C’, ’D’, ‘E’, ‘F’};
String[] villages = {”Mochudi”, ”Serowe”, ”Kanye”}
10
4. Adding Values to an Array
An array object has the attribute length, which is public.
• Its value is the number of cells in the array.
◦ WARNING!!! This is not a method!!!
Syntax (for adding a value):
arrayName[intExp] = expression;
• intExp is an integral expression, called the index, satisfying
0 <= intExp <= arrayName.length − 1.
Notice that arrayName[intExp] is used as a variable!
int n =10;
int[] numbers = new int[n+4];
for (int i = 0; i < numbers.length; i++){
if ( i%2 ==1)
numbers[i] = i+1;
} 11
The Big Picture
numbers
50
50
2 4 6 8 10 12 14
0 1 2 3 4 5 6 7 8 9 10 11 12 13
12
5. Array Index Out of Bounds
Array IndexOutOfBounds:
An error you get if you try to access a non-existent
cell of the array.
Can only be caught at run-time.
Example
int[] numbers = new int[10];
Example
public class PassArrays {
// method returns true if x is in the array listA
public boolean search (int[] listA, int x) {
boolean found = false;
for (int i= 0; i<listA.length; i++){
if (listA[i] == x )
found = true;
}
return found;
}
} 15
To use the method:
16
a) Initializing an Array to a Specific Value
public void initArray (int[] numbers, int initValue) {
for (int i = 0; i < numbers.length; i++){
numbers[i] = initValue;
}
}
Method invoked by giving it an (integer) array and an
integer value to initialize the array cells with.
• The array numbers MUST have been created,
otherwise NullPointerException will be raised.
17
b) Reading Data into an Array
public void readIntoArray(int[ ] numbers, Scanner sc)
for (int i = 0; i < numbers.length; i++){
numbers[i] = sc.nextInt();
}
}
Method invoked by giving/passing it an (integer)
array AND a Scanner object.
• Both the array and the Scanner object MUST have
been created.
18
c) Printing an Array
public void printArray (int[ ] numbers) {
System.out.printf(“{"); // for formatting output
for (int i = 0; i < numbers.length; i++){
System.out.printf(“%d ”, numbers[i])
}
System.out.printf(“}");
}
Method invoked by giving it an (integer) array.
• The array MUST have been created.
19
d) Determining Index of the Largest Element
public int indexOfMax(int[ ] numbers) {
int index = 0; //Assume numbers[0] has max
for (int i = 0; i < numbers.length; i++){
if (numbers[i] > numbers[index])
index = i;
}
return index;
}
Method invoked by giving it an (integer) array.
• The array MUST have been created.
It returns an integer indicating the position of the largest
element in the array.
20
e) Determining the Average
public double computeArrayAverage( int[ ] numbers) {
double sum = 0.0;
double average = 0.0;
for (int i=0; i < numbers.length; i++){
sum = sum + numbers[i];
}
if (numbers.length != 0)
average = sum/numbers.length;
return average;
}
Method invoked by giving it an (integer) array.
• The array MUST have been created.
It returns average of elements in the array. 21
8. Arrays of String Objects
Declaring:
String[] classList = new String[5];
Example:
Dog[] dogs = new Dog[5];
System.out.printf(“%d\n”, dogs[0].getName());
System.out.printf(“%s\n”,dogs[0]. computeDogYears());
23
Another Example:
24
Summary
1. Collections
2. One Dimensional Arrays
3. Array Index Out of Bounds Error
4. Declaring Arrays as Formal Parameters
5. Processing One Dimensional Arrays with Methods
6. Arrays of Objects
25