CS 01 Chapter 5
CS 01 Chapter 5
Prepared by:
DANILYN A. FLORES
NOR-AINE M. CORPUZ
CS 01 1-2022-2023 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
CS 01 1-2022-2023 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…
CS 01 1-2022-2023 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.
CS 01 1-2022-2023 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;
CS 01 1-2022-2023 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.
CS 01 1-2022-2023 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.
CS 01 1-2022-2023 7
Introduction to Arrays
A regular variable
num: 1
An array variable
0 1 2
num:
1 3 5
CS 01 1-2022-2023 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[ ];
CS 01 1-2022-2023 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];
CS 01 1-2022-2023 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
…
CS 01 1-2022-2023 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”};
CS 01 1-2022-2023 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.
CS 01 1-2022-2023 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.
CS 01 1-2022-2023 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,
CS 01 1-2022-2023 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]); } } }
CS 01 1-2022-2023 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)
CS 01 1-2022-2023 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
CS 01 1-2022-2023 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++) {
CS 01 1-2022-2023
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?
CS 01 1-2022-2023 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
…
CS 01 1-2022-2023 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”}};
CS 01 1-2022-2023 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,
CS 01 1-2022-2023 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]); } } } }
CS 01 1-2022-2023 28
LESSON ACTIVITY 5.4
1. Directory
Create a program 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.
Example Output: Name : Angelina Jolie
Telephone No.: 572-1234
Address: Hollywood
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 30
LESSON ACTIVITY 5.4
3. X and Y Axis Points
Create a program 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.
CS 01 1-2022-2023 31
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
CS 01 1-2022-2023 32
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.
CS 01 1-2022-2023 33
Internet Resources
• http://www.learntoprogramming.com/data-structure-using-c/applications-
arrays
• https://www.thecrazyprogrammer.com/2020/04/applications-of-array.html
• https://unifiedinfotech.wordpress.com/2017/02/14/does-using-array-
facilitate-android-application-development/
CS 01 1-2022-2023 34
THANK YOU!
Stay safe. #WeHealAsOne
CS 01 1-2022-2023 35