AMath 04 CHAPTER 5 Arrays
AMath 04 CHAPTER 5 Arrays
Prepared by:
DANILYN A. FLORES
NOR-AINE M. CORPUZ
AMath 04 1-2023-2024 1
LEARNING OUTCOMES
1. Define arrays
2. Determine the output of a specific program that use arrays
3. Develop an interactive program using and accessing arrays of
different types and length to solve problems
4. Develop an interactive program using and accessing
multidimensional arrays of different types and length to solve
problems
AMath 04 1-2023-2024 2
APPLICATIONS OF ARRAYS
• Databases - small and large databases consist of one-dimensional arrays whose
elements are records.
• Android Applications Development – arrays forms a fundamental part to
develop an android based application.
• Data Structures – arrays are used to implement other data structures, such as
lists, heaps, hash tables, deques, queues and stacks.
• Sorting Algorithms and many more…
AMath 04 1-2023-2024 3
Introduction to Arrays
In the previous chapters, it was discussed how to declare different
variables using different data types. In declaring variables, a unique
identifier name and data type should be used. In order to use the
variable, its identifier name is called.
AMath 04 1-2023-2024 4
Introduction to Arrays
For example, there are three (3) variables of type int with different names for
each variable:
int num1 = 0;
int num2 = 0;
int num3 = 0;
Each variable can only hold one value each time it is updated.
num1 = 1;
num2 = 3;
num3 = 5;
AMath 04 1-2023-2024 5
Introduction to Arrays
It seems like a tedious task in order to initialize or assign values and
use the variables. In Java and other programming languages, there is
one capability wherein one variable can store a list of data and
manipulate them more efficiently. This type of variable is called an
array.
AMath 04 1-2023-2024 6
Introduction to Arrays
An array stores multiple data items of the same data type, in a
contiguous block of memory, divided into a number of slots. Think
of an array as a stretched variable – a location that still has one
identifier name, but can hold more than one value. For example you
can declare an int array variable named num that can hold three (3)
values: 1, 3, and 5 instead of declaring three (3) variables that can
hold one (1) value each like in the previous example.
AMath 04 1-2023-2024 7
Introduction to Arrays
A regular variable
num: 1
An array variable
0 1 2
num:
1 3 5
AMath 04 1-2023-2024 8
Declaring Arrays
Arrays must be declared like all variables. To declare an array, list
the data type, followed by a set of square brackets [ ], and followed
by the identifier name. For example,
int [ ]num;
The square brackets can also be placed after the identifier name.
For example,
int num[ ];
AMath 04 1-2023-2024 9
Declaring Arrays
After declaring, the array must be created and its length must be
specified with a constructor statement. This process in Java is
called instantiation (the Java word for create). Take note that the
size of the array cannot be changed once initialized. For example,
int num[ ];
num = new int[100];
or
int [ ]num = new int[100];
AMath 04 1-2023-2024 10
Declaring Arrays
In the example, the declaration tells the Java compiler that the
identifier num will be used as the name of an array containing
integers and to create a new array containing 100 elements.
0 1 2 3 4 99
num
…
AMath 04 1-2023-2024 11
Declaring Arrays
Instead of using the new keyword to instantiate an array, it can also
be automatically declared, created and assigned values at once with
the elements enclosed in a pair of curly braces ( { } ) and separated
by commas ( , ).
For example,
boolean results[ ] = {true, false, true, false};
double [ ]grades = {100, 90.99, 80.25, 75.0};
String days[ ] = {“Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”,”Sun”};
AMath 04 1-2023-2024 12
LESSON ACTIVITY 5.1
1. Declare a string array named holidays with a length of 5.
2. Declare an int array named ages and initialize to the ages of you and your
3 friends.
AMath 04 1-2023-2024 13
Accessing an Array Element
• To access an array element, or a part of the array, a number called
an index or a subscript is used.
• An index or a subscript is assigned to each member of the
array, allowing the program and the programmer to access
individual values when necessary. Index numbers are always
integers.
• They begin with a zero (0) and progress sequentially by whole
numbers to the end of the array.
AMath 04 1-2023-2024 14
Accessing an Array Element
Take note that the index of the elements inside an array is from 0 to
(size of the array – 1).
For example, given the array declared earlier,
AMath 04 1-2023-2024 18
Accessing an Array Element
public class ArraySample_2{
public static void main(String [ ]args) {
int [ ]num = new int[100];
for(int i = 0; i<100; i++) {
System.out.print(num[i]); } } }
AMath 04 1-2023-2024 19
LESSON ACTIVITY 5.2
1. Assign “Christmas Day” as the value of the last element of array holidays.
(Lesson Activity 5.1 #1)
2. Display the 1st element of array ages. (Lesson Activity 5.1 #2)
AMath 04 1-2023-2024 20
Array Length
In order to get the number of elements in an array, the length field
of an array can be used. The length field of an array returns the
size of the array. It can be used by writing,
<arrayName>.length
AMath 04 1-2023-2024 21
Array Length
For example, given the code in the previous example, it can be
rewritten as,
Below is another example where a named variable is used as the declared size
of an array so as to make the size easy to change.
public class ArraySample{
public static void main(String []args) {
int x = 100;
int num[ ] = new int[x];
for(int i = 0; i<num.length; i++) {
AMath 04 1-2023-2024
System.out.print(num[i]); } } } 23
LESSON ACTIVITY 5.3
int size = 3;
String course[ ] = new String[size];
for(int i = 0; i<course.length; i++) { . . . }
1. What value will the length field return in the code fragment
given above?
AMath 04 1-2023-2024 24
Multidimensional Arrays
Multidimensional arrays are implemented as arrays of arrays.
Multidimensional arrays are declared by placing the appropriate
number of bracket pairs before or after the array name.
A 2-dimensional array
num 0 1 …
0
1
…
AMath 04 1-2023-2024 25
Multidimensional Arrays
For example,
int [ ][ ]twoD = new int [512][128];
char threeD[ ][ ][ ] = new char[8][16][24];
String [ ][ ]dogs = {{“Terry”,”Brown”},
{“Rave”,”White”},
{“Toby”,”Gray”,},
{“Fido”,”Black”}};
AMath 04 1-2023-2024 26
Multidimensional Arrays
Accessing an array element in a multidimensional array is just the
same as accessing the elements in a one dimensional array. For
example, to access the first element in the first row of the array
dogs, write,
AMath 04 1-2023-2024 27
Multidimensional Arrays
The following is an example on how to print the elements of a
multidimensional array using loops.
public class ArraySample{
public static void main(String []args) {
String [][]dogs={{“Terry”,”Brown”},{“Rave”,”White”},
{“Toby”,”Gray”}, {“Fido”,”Black”}};
for(int i = 0; i<dogs.length; i++) {
for(int j = 0; j<dogs[i].length; j++){
System.out.print(dogs[i][j]); } } } }
AMath 04 1-2023-2024 28
LESSON ACTIVITY 5.4
1. Name Counter
Create a program that will count the number of times an input name appeared
in a list from an input array of 10 names.
Note: The .equals() method is used to compare 2 strings. Result is either true or false.
<str1>.equals(<str2>)
Example:
word1.equals(word2) or “Happy”.equals(“happy”)
CS 01 1-2022-2023 29
FORMATIVE ASSESSMENT 1
1. Directory
Create a program (Design and Coding)that will prompt the user to enter 5
names, telephone numbers, and addresses, store the data in an array variable
and display like a directory.
See next slide for sample output.
AMath 04 1-2023-2024 30
FORMATIVE ASSESSMENT 1
AMath 04 1-2023-2024 31
FORMATIVE ASSESSMENT 1
2. X and Y Axis Points
Create a program (Design and
Coding) that will prompt for 5 pairs
of x and y axis points, store them in a
multidimensional array, display the
points and its corresponding
quadrant in the Cartesian plane like in
the sample output shown.
AMath 04 1-2023-2024 32
CHAPTER SUMMARY
An array is a variable that stores multiple data items of the same data type
used to create shorter functional codes
Arrays can be multidimensional
Arrays are one of the ways that can help programmers write shorter but
functional codes
AMath 04 1-2023-2024 33
ANSWER KEY
AMath 04 1-2023-2024 34
Formative
Assessment 1
1.
AMath 04 1-2023-2024 35
Formative Assessment 1
AMath 04 1-2023-2024 36
Formative
Assessment 1
2.
AMath 04 1-2023-2024 37
Formative
Assessment 1
Program Coding
AMath 04 1-2023-2024 38
References
[1] Burd, B. (2017). Beginning Programming with Java For Dummies (5th Ed). New Jersey:
John Wiley and Sons, Inc.
[2] Cavida, D.G, Frio, F.M., Flores, D. (2010). Computer Programming I. Unpublished
Workbook, University of Southern Mindanao, Kabacan, Cotabato.
[3] Flask, R. (ND). Java for Beginners 2nd Edition [PDF File]. Retrieved from
http://staff.um.edu.mt/__data/assets/pdf_file/0010/57169/jn.pdf
[4] Mayfield, B. (2016). From Problem Analysis to Program Design Lab Manual (3rd ed.).
Philippines: Cengage Learning Asia Pte Ltd.
AMath 04 1-2023-2024 39
THANK YOU!
Stay safe. #WeHealAsOne
AMath 04 1-2023-2024 40