Chapter 6: Array
Object Oriented Programming
Learn about arrays
Explore how to declare and manipulate data into arrays
Understand the meaning of “array index out of bounds”
Learning Become familiar with the restrictions on array processing
Objectives Discover how to pass an array as a parameter to a method
Discover how to manipulate data in a two-dimensional array
Learn about multidimensional arrays
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 2
Definition: structured data type with a fixed number of
elements
Elements of an array are also called components of the
Array array
Every element is of the same type
Elements are accessed using their relative positions in the
array
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 3
One-
Dimensional
Arrays
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 4
One-
Dimensional
Arrays
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 5
One-
Dimensional − intExp = number of components in array >= 0
− 0 <= indexExp <= intExp
Arrays
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 6
Array num:
− int[] num = new int[5];
One-
Dimensional
Arrays
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 7
One-
Dimensional
Arrays
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 8
One-
Dimensional
Arrays
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 9
One-
Dimensional
Arrays
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 10
One-
Dimensional
Arrays
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 11
Specifying Array
Size During
Program
Execution
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 12
Array
Initialization The initializer list contains values, called initial values, that
During are placed between braces and separated by commas
Declaration Here, sales[0]= 12.25, sales[1]= 32.50, sales[2]= 16.90,
sales[3]= 23.00, and sales[4]= 45.68
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 13
Array When declaring and initializing arrays, the size of the array is
Initialization determined by the number of initial values within the braces
During If an array is declared and initialized simultaneously, we do
not use the operator new to instantiate the array object
Declaration
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 14
Associated with each array that has been instantiated, there
Arrays and the is a public (final) instance variable length
Instance The variable length contains the size of the array
Variable length The variable length can be directly accessed in a program
using the array name and the dot operator
int[] list = {10, 20, 30, 40, 50, 60};
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 15
This statement creates the array list of six components and
initializes the components using the values given
− Here [Link] is 6
Arrays and the
Instance int[] numList = new int[10];
Variable length This statement creates the array numList of 10 components
and initializes each component to 0
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 16
The value of [Link] is 10
− numList[0] = 5;
− numList[1] = 10;
− numList[2] = 15;
− numList[3] = 20;
Arrays and the
These statements store 5, 10, 15, and 20, respectively, in
Instance the first four components of numList
Variable length You can store the number of filled elements, that is, the
actual number of elements, in the array in a variable, say
numOfElement
It is a common practice for a program to keep track of the
number of filled elements in an array
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 17
Loops used to step through elements in array and perform
operations
int[] list = new int[100];
int i;
Processing One- for (i = 0; i < [Link]; i++)
Dimensional //process list[i], the (i + 1)th
//element of list
Arrays for (i = 0; i < [Link]; i++)
list[i] = [Link]();
for (i = 0; i < [Link]; i++)
[Link](list[i] + " ");
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 18
Some operations on arrays:
− Initialize
− Input data
− Output stored data
− Find largest/smallest/sum/average of elements
Arrays cont…
double[] sales = new double[10];
int index;
double largestSale, sum, average;
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 19
Code to
Initialize Array for (index = 0; index < [Link]; index++)
sales[index] = 10.00;
to Specific
Value (10.00)
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 20
Code to Read for (index = 0; index < [Link]; index++)
Data into Array sales[index] = [Link]();
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 21
Code to Print for (index = 0; index < [Link]; index++)
Array [Link](sales[index] + " ");
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 22
sum = 0;
Code to Find for (index = 0; index < [Link]; index++)
sum = sum + sales[index];
Sum and
Average of if ([Link] != 0)
average = sum / [Link];
Array else
average = 0.0;
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 23
maxIndex = 0;
Determining for (index = 1; index < [Link]; index++)
Largest if (sales[maxIndex] < sales[index])
maxIndex = index;
Element in
largestSale = sales[maxIndex];
Array
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 24
Determining
Largest
Element in
Array
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 25
Array in bounds if:
− 0 <= index <= arraySize – 1
Array Index Out If index < 0 or index > arraySize:
of Bounds − ArrayIndexOutOfBoundsException exception is thrown
Base address: memory location of first component in array
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 26
A general syntax to declare an array as a formal parameter
− dataType[] arrayName
Declaring public static void arraysAsFormalParameter(int[] listA,
Arrays as {
double[] listB, int num)
//...
Formal }
int[] intList = new int[10];
Parameters to double[] doubleNumList = new double[15];
int number;
Methods arraysAsFormalParameter(intList, doubleNumList, number);
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 27
The
Assignment
Operators and
Arrays
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 28
The
Assignment
Operators and
Arrays
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 29
The
Assignment
Operators and
Arrays
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 30
if (listA == listB)...
− The expression listA == listB determines if the values of listA and
listB are the same and thus determines whether listA and listB refer
Relational to the same array
Operators − To determine whether listA and listB contain the same elements,
you need to compare them component by component
Arrays − You can write a method that returns true if two int arrays contain
the same elements
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 31
boolean isEqualArrays(int[] firstArray,
int[] secondArray)
{
if ([Link] != [Link])
return false;
Relational for (int index = 0; index < [Link];
index++)
Operators if (firstArray[index] != secondArray[index])
Arrays return false;
return true;
}
if (isEqualArrays(listA, listB))
...
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 32
Arrays as
Parameter
Methods
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 33
Methods for
Array
Processing
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 34
Methods for
Array
Processing
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 35
Methods for
Array
Processing
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 36
Methods for
Array
Processing
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 37
Can use arrays to manipulate objects
Example: create array named array1 with N objects of type T
Arrays of − T[] array1 = new T[N]
Objects Can instantiate array1 as follows:
− for(int j=0; j <[Link]; j++)
array1[j] = new T();
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 38
String[] nameList = new String[5];
nameList[0] = "Amanda Green";
Array of String nameList[1] = "Vijay Arora";
Objects nameList[2] = "Sheila Mann";
nameList[3] = "Rohit Sharma";
nameList[4] = "Mandy Johnson";
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 39
Array of String
Objects
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 40
Clock[] arrivalTimeEmp = new Clock[100];
Arrays of
Objects
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 41
for (int j = 0; j < [Link]; j++)
arrivalTimeEmp[j] = new Clock();
Arrays of
Objects
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 42
arrivalTimeEmp[49].setTime(8, 5, 10);
Arrays of
Objects
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 43
Arrays and The syntax to declare a variable length formal parameter
Variable Length (list) is:
− dataType ... identifier
Parameter List
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 44
Arrays and
Variable Length
Parameter List
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 45
Arrays and
Variable Length
Parameter List
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 46
A method can have both a variable length formal parameter
and other formal parameters; consider the following method
heading:
− public static void myMethod(String name, double num, int ... intList)
The formal parameter name is of type String, the formal
Arrays and parameter num is of type double, and the formal parameter
intList is of variable length
Variable Length The actual parameter corresponding to intList can be an int
Parameter List array or any number of int variables and/or int values
A method can have at most one variable length formal
parameter
If a method has both a variable length formal parameter and
other types of formal parameters, then the variable length
formal parameter must be the last formal parameter of the
formal parameter list
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 47
The syntax to use this for loop to process the elements of an
array is:
− for (dataType identifier : arrayName)
statements
foreach loop
identifier is a variable and the data type of identifier is the
same as the data type of the array components
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 48
sum = 0;
for (double num : list)
sum = sum + num;
The for statement in Line 2 is read: for each num in list
The identifier num is initialized to list[0
foreach loop In the next iteration, the value of num is list[1], and so on
for (double num : numList)
{
if (max < num)
max = num;
}
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 49
Two-
Dimensional
Arrays
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 50
Two-
Dimensional
Arrays
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 51
double[][] sales = new double[10][5];
Two-
Dimensional
Arrays
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 52
Two-
Dimensional intExp1, intExp2 >= 0
Arrays indexExp1 = row position
indexExp2 = column position
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 53
Two-
Dimensional
Arrays
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 54
This statement declares and instantiates a two-dimensional
array matrix of 20 rows and 15 columns
The value of the expression:
Two- − [Link]
Dimensional − is 20, the number of rows
Arrays and the Each row of matrix is a one-dimensional array; matrix[0], in
Instance fact, refers to the first row
Variable length The value of the expression:
− matrix[0].length
− is 15, the number of columns in the first row
matrix[1].length gives the number of columns in the second
row, which in this case is 15, and so on
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 55
Two-
Dimensional
Arrays: Special
Cases
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 56
Two-
Dimensional Create columns
Arrays: Special
Cases
(continued)
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 57
Two-
Dimensional
Array
Initialization
During
Declaration
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 58
Two-
Dimensional To initialize a two-dimensional array when it is declared
Array − The elements of each row are enclosed within braces and
Initialization separated by commas
− All rows are enclosed within braces
During
Declaration
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 59
Two-
Dimensional
Array
Initialization
During
Declaration
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 60
Three ways to process 2-D arrays
Two- − Entire array
− Particular row of array (row processing)
Dimensional − Particular column of array (column processing)
Arrays Processing algorithms similar to processing algorithms of
one-dimensional arrays
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 61
Initialization
for (row = 0; row < [Link]; row++)
Two- for (col = 0; col < matrix[row].length; col++)
matrix[row][col] = 10;
Dimensional Print
Arrays: for (row = 0; row < [Link]; row++)
{
Processing for (col = 0; col < matrix[row].length; col++)
[Link]("%7d", matrix[row][col]);
[Link]();
}
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 62
Input
for (row = 0; row < [Link]; row++)
for (col = 0; col < matrix[row].length; col++)
matrix[row][col] = [Link]();
Two- Sum by Row
Dimensional for (row = 0; row < [Link]; row++)
{
Arrays: sum = 0;
for (col = 0; col < matrix[row].length; col++)
Processing sum = sum + matrix[row][col];
[Link]("Sum of row " + (row + 1)
+ " = "+ sum);
}
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 63
Sum by Column
Two- for (col = 0; col < matrix[0].length; col++)
{
Dimensional sum = 0;
for (row = 0; row < [Link]; row++)
Arrays: sum = sum + matrix[row][col];
[Link]("Sum of column " + (col + 1)
Processing + " = " + sum);
}
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 64
Largest Element in Each Row
for (row = 0; row < [Link]; row++)
Two- {
Dimensional largest = matrix[row][0];
for (col = 1; col < matrix[row].length; col++)
Arrays: if (largest < matrix[row][col])
largest = matrix[row][col];
Processing [Link]("The largest element of row "
+ (row + 1) + " = " + largest);
}
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 65
Largest Element in Each Column
for (col = 0; col < matrix[0].length; col++)
Two- {
largest = matrix[0][col];
Dimensional for (row = 1; row < [Link]; row++)
if (largest < matrix[row][col])
Arrays: largest = matrix[row][col];
[Link]("The largest element of col "
Processing + (col + 1) + " = " + largest);
}
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 66
Can define three-dimensional arrays or n-dimensional array
(n can be any number)
Syntax to declare and instantiate array
Multi − dataType[][]…[] arrayName = new
Dimensional − dataType[intExp1][intExp2]…[intExpn];
Arrays Syntax to access component
− arrayName[indexExp1][indexExp2]…[indexExpn]
intExp1, intExp2, ..., intExpn = positive integers
indexExp1,indexExp2, ..., indexExpn = non-negative integers
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 67
Loops to double[][][] carDealers = new double[10][5][7];
for (i = 0; i < 10; i++)
Process Multi for (j = 0; j < 5; j++)
for (k = 0; k < 7; k++)
Dimensional carDealers[i][j][k] = 10.00;
Arrays
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 68
Program: reads given text; outputs the text as is; prints
number of lines and number of times each letter appears in
Programming text
Example: Input: file containing text to be processed
Text Processing Output: file containing text, number of lines, number of
times letter appears in text
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 69
An array of 26 representing the letters in the alphabet
Three methods
Programming − copyText
Example: − characterCount
− writeTotal
Text Processing
Value in appropriate index incremented using methods and
depending on character read from text
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 70
Arrays
− Definition
− Uses
Summary Different Arrays
− One-dimensional
− Two-dimensional
− Multidimensional (n-dimensional)
− Arrays of objects
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 71
Declaring arrays
Instantiating arrays
Processing arrays
− Entire array
Summary − Row processing
− Column processing
Common operations and methods performed on arrays
Manipulating data in arrays
Engr. Kris C. Calpotura, MSME, MIT Chapter 6: Array 72
The End!!!