4.
0
JAVA LANGUAGE
COMPUTER SCIENCE
SC015
4.4 ARRAY
Learning Outcome
At the end of lesson, students should be able to :
a) Explain an array and its components. (L)
b) Declare array reference variables and create array. (L)
c) Accessing array elements : (L)
• initialize
• input
• process and
• output
d) Construct programs that perform one-dimensional array
operations for problem involving linear search. (T)
e) Construct programs that perform one-dimensional array operations for
problems involving total and frequency. (P)
INTRODUCTION
• Suppose you need to read 100 numbers, compute their
average, and find out how many numbers are above
the average.
• You have to declare 100 variables and repeatedly write
almost identical code 100 times. Writing a program this
way would be impractical.
INTRODUCTION
How do you solve this problem?
• Java and most other high-level languages provide
the array, which stores a fixed-size sequential
collection of elements of the same type.
• In the present case, you can store all 100 numbers
into an array and access them through a single
array variable.
DEFINITION
• An array is an ordered sequence of data of the
same type.
• A single array variable can reference a large
collection of data.
age 30 15 88 100 6 Hold integer value
height 1.4 2.0 0.7 1.9 1.5 1.0 Hold decimal value
gender ‘m’ “male” ‘F’ ‘F’ 1.6 ‘f’ Mix value
subject “Comp” “Chem” “Math” “Bio” Hold string value
nationality true false false true true Hold boolean value
ARRAY COMPONENTS
• Generally, an array consists of :
• Array name (any valid variable name)
• Data type (integer, char and etc.)
• Size (bigger size is better)
• Index (start from 0 to size -1)
• Value (based on data type)
Example :
int [] arr = new int [7];
Memory value
73 1 0 4 5 3 2
location
[0] [1] [2] [3] [4] [5] [6] index
arr Array name
4.4 ARRAY
Learning Outcome
At the end of lesson, students should be able to :
a) Explain an array and its components. (L)
b) Declare array reference variables and create array. (L)
c) Accessing array elements : (L)
• initialize
• input
• process and
• output
d) Construct programs that perform one-dimensional array
operations for problem involving linear search. (T)
e) Construct programs that perform one-dimensional array operations for
problems involving total and frequency. (P)
DECLARE AND CREATE ARRAY
DECLARING AN ARRAY
• Declare a variable that refers to the array
should indicate the type of elements stored by the
array.
elementType[ ] arrayRefVar;
elementType
- int arrayRefVar
- float []
- double - Indicate for array - Any valid variable name
- char declaration
- boolean
DECLARE AND CREATE ARRAY
DECLARING AN ARRAY
Examples :
int[] array1; // an array of int elements
double[] temp; // an array of double elements
char[] gender; // an array of char elements
boolean[] answer; // an array of boolean elements
DECLARE AND CREATE ARRAY
CREATING AN ARRAY
• Indicates how many positions the array should hold
(size of array)
array1 = new int[10]; // array hold 10 elements
weight = new float[20]; // array hold 20 elements
temp = new double[100]; // array hold 100 elements
gender = new char[75]; // array hold 75 elements
answer = new boolean[50]; // array hold 50 elements
DECLARE AND CREATE ARRAY
• Declaring and creating an array at the same time :
int[] array1 = new int[10];
float[] weight = new float [20];
double[] temp = new double[100];
char[] gender = new char[75];
boolean[] answer = new boolean[50];
int[] array1;
int[] array1 = new int[10];
array1 = new int[10];
Checkpoint 1
1. Define array.
___________________________________
___________________________________
2. Determine component the following declaration
statement.
double[] finalMark;
________________________ __________
___________________________________
Checkpoint 1
3. Write a Java code segment to perform the
following tasks :
Declare an array named pHvalue of type
double.
_____________________________________________
4. Declare and create array named marks to store
100 students’ marks.
_____________________________________________
4.4 ARRAY
At the end of lesson, students should be able to :
a) Explain an array and its components. (L)
b) Declare array reference variables and create array. (L)
c) Accessing array elements : (L)
• initialize
• input
• process and
• output
d) Construct programs that perform one-dimensional array
operations for problem involving linear search. (T)
e) Construct programs that perform one-dimensional array operations for
problems involving total and frequency. (P)
ACCESSING ARRAY
ARRAY INDEX
• The array elements are accessed through the index.
• In Java, the index of an array always start with 0. They
never start with 1 or any number other than 0.
• If the length of an array is n, the last element will be
arrayName[n-1].
ACCESSING ARRAY
ARRAY INDEX
int[] age = new int[5];
ACCESSING ARRAY
INITIALIZING ARRAY ELEMENTS
• Initialize an array by assigning values one by one :
double[] temp = new double[5];
temp[0] = 33.3;
temp[1] = 31.5;
1 temp[2] = 29.0;
temp[3] = 30.2;
temp[4] = 38.7;
ACCESSING ARRAY
INITIALIZING ARRAY ELEMENTS
• Use the following shorthand:
2 double[] temp = {33.3, 31.5, 29.0, 30.2, 38.7};
ACCESSING ARRAY
INPUT ARRAY ELEMENTS
• Initialization of array elements can be made reading
value into array (user input).
Scanner sc = new Scanner([Link]);
double[] temp = new double[5];
temp[0] = [Link]();
1 temp[1] = [Link]();
temp[2] = [Link]();
temp[3] = [Link]();
temp[4] = [Link]();
ACCESSING ARRAY
INPUT ARRAY ELEMENTS
• Frequently, arrays are processed within for loops.
• We can write a repetition code (using for or while) to read
values into an array.
• Object name “.length” will store size of an array
Scanner sc = new Scanner([Link]);
double[] temp = new double[7];
2 for (int i = 0;i < [Link];i = i + 1) // using for loop
{
temp[i] = [Link]();
}
ACCESSING ARRAY
INPUT ARRAY ELEMENTS
Scanner sc = new Scanner([Link]);
double[] temp = new double[7];
int i = 0;
3 while (i < [Link]) // using while loop
{
temp[i] = [Link]();
i = i + 1;
}
ACCESSING ARRAY
PROCESS ARRAY ELEMENTS
• All array elements can be processed using arithmetic,
logic and relational operators.
Consider :
int [] age = {12, 4, 5, 2, 5};
i. age [1] = age[4] – age[0];
ii. if (age2] > 3)
iii. if (age[1] == 4 && age [3] ==2)
ACCESSING ARRAY
OUTPUT (DISPLAY) ARRAY ELEMENTS
• To print an array, you have to print each element in the
array or using a loop like the following:
int [] age = {12, 4, 5, 2, 5}; equivalent to
[Link](age[0);
[Link](age[1]);
[Link](age[2]);
[Link](age[3]); int [] age = {12, 4, 5, 2, 5};
[Link](age[4]); for (int i = 0; i < [Link]; i = i + 1)
{
[Link](age[i]);
}
ACCESSING ARRAY
OUTPUT (DISPLAY) ARRAY ELEMENTS
• To print an array, you have to print each element in the
array or using a loop like the following:
int [] age = {12, 4, 5, 2, 5}; equivalent to
[Link](age[0]);
[Link](age[1]);
[Link](age[2]);
int [] age = {12, 4, 5, 2, 5};
[Link](age[3]); int i = 0;
while ( i < [Link])
[Link](age[4]);
{
[Link](age[i]);
i = i +1;
}
Checkpoint 2
1. Consider the following array.
double [ ] pressure = new double [10];
i. State the value of [Link]
_________________________________
ii. State the first index of array pressure.
_________________________________
iii. State the highest index of array pressure.
_________________________________
Checkpoint 2
2. Declare, create and initialize an array named reading
that hold double values : 3.3, 15.8 and 9.7.
_______________________________________________________
3. Consider the following array.
double[ ] price = new double[15];
Write a statement to:
a) Assign the value 5.5 to the last element in the array
price.
____________________________________________________
Checkpoint 2
b) Write a program segment to enter data into the array
named bookPrice with array size is 25 and hold
decimal values .
Checkpoint 2
c). Consider the following code :
double tide[] = {12.2, −7.3, 14.2, 11.3};
[Link]("Tide 1 is " + tide[1]);
[Link]("Tide 2 is " + tide[2]);
i) What output will be produced ?
_________________________________
_________________________________
Checkpoint 2
ii). Write program segment to display all elements in array tide
4.4 ARRAY
Learning Outcome
At the end of lesson, students should be able to :
a) Explain an array and its components. (L)
b) Declare array reference variables and create array. (L)
c) Accessing array elements : (L)
• initialize
• input
• process and
• output
d) Construct programs that perform one-dimensional array
operations for problem involving linear search. (T)
e) Construct programs that perform one-dimensional array operations for
problems involving total and frequency. (P)
Linear Search in Array
Linear search or sequential search is a method for
finding an element within a list. It sequentially checks
each element of the list until a match is found or the
whole list has been searched.
Linear Search in Array
This program requires user to input a
value to search. If a match is found then
EXAMPLE 01 the index of the element is displayed,
otherwise message "Element not found“
is displayed.
Linear Search in Array
import [Link];
public class LinearSearch
{
public static void main(String[] args)
{
Scanner input = new Scanner([Link]);
int aray[] = {10, 20, 25, 63, 96, 57}; When you run the program,
[Link]("Enter value to search: "); the output will be :
int search = [Link]();
int found = 0;
for (int i=0 ;i< [Link]; i = i + 1)
{
if(aray[i]==search)
{
[Link]("Element found index is :"+ i);
found = 1;
}
}
if (found == 0)
{
[Link]("Element not found");
}
}
}
Linear Search in Array
This program segment requires user to
input
This a value towill
program search. If a
display match is
odd
EXAMPLE 01
EXAMPLE 02 found theninthe
element anindex
[Link] the element is
displayed, otherwise message
"Element not found“ is displayed.
Linear Search in Array
import [Link];
public class FindOdd
{
public static void main(String[] args) When you run the program,
{ the output will be:
int aray[] = {10, 20, 25, 63, 96, 57};
int odd = 0;
[Link] (" Odd are ");
for(int i=0 ;i< [Link]; i = i + 1)
{
if(aray[i]% 2 != 0)
{
[Link](aray[i]+" ");
odd = 1;
}
}
if(odd == 0)
{
[Link]("No odd element");
}
}
}
Linear Search in Array
This program will display elements
EXAMPLE 03 in array which greater than 50.
Linear Search in Array
import [Link];
public class LinearSearch3
{
public static void main(String[] args)
{
When you run the program, the
int aray[] = {10, 20, 25, 63, 96, 57}; output will be:
int great50 = 0;
[Link]("Elements greater than 50 are: ");
for (int i=0 ;i< [Link]; i = i + 1)
{
if(aray[i]>50)
{
[Link](aray[i]+" ");
great50 = 1;
}
}
if (great50 == 0)
{
[Link]("No element greater than 50");
}
}
}
4.4 ARRAY
Learning Outcome
At the end of lesson, students should be able to :
a) Explain an array and its components. (L)
b) Declare array reference variables and create array. (L)
c) Accessing array elements : (L)
• initialize
• input
• process and
• output
d) Construct programs that perform one-dimensional array
operations for problem involving linear search. (T)
e) Construct programs that perform one-dimensional array operations for
problems involving total and frequency. (P)
01 Array operations Total
import [Link];
public class Total
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
double myList[] = new double[7];
double total = 0;
for (int i = 0; i < [Link]; i = i + 1)
{
[Link]("Enter number ");
myList[i] = [Link]();
total = total + myList[i];
}
[Link]("Total is "+total);
}
}
02 Array operations Frequency
• Frequency to count the occurrence of certain events :
import [Link];
public class Frequency
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
double myList[] = new double[5];
int odd = 0, even = 0;
for (int i = 0; i < [Link]; i = i + 1)
{
[Link]("Enter a number ");
myList[i] = [Link]();
if (myList[i] % 2 == 0)
even = even + 1;
else
odd = odd + 1;
}
[Link]("Even number appear "+even+" times");
[Link]("Odd number appear "+odd+" times");
}
}
4.6 JAVA PROGRAM
Learning Outcome
At the end of lesson, students should be able to :
a) Construct programs that perform one dimensional array
operations for problems involving average, maximum
and minimum. (T)
01 Array operations Average
import [Link];
public class Mean
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
double myList[] = new double[7];
double total = 0.0,average;
for (int i = 0; i < [Link]; i = i + 1)
{
[Link]("Enter number ");
myList[i] = [Link]();
total = total + myList[i];
}
average = total/7;
[Link]("Mean is "+average);
}
}
02 Array operations Maximum
• To find the maximum value from series of numbers
import [Link];
public class Maximum
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
int myList[] = new int[7];
int max;
[Link]("Enter first number");
myList[0] = [Link]();
max = myList[0];
for (int i = 1; i < [Link]; i = i + 1)
{
[Link]("Enter next number");
myList[i] = [Link]();
if (myList[i] > max)
max = myList[i];
}
[Link]("Maximum number is " +max);
}
}
03 Array operations Minimum
• To find the minimum value from series of numbers
import [Link];
public class Minimum
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
int myList[] = new int [7];
int min;
[Link]("Enter first number");
myList[0] = [Link]();
min = myList[0];
for (int i = 1; i < [Link]; i = i + 1)
{
[Link]("Enter next number");
myList[i] = [Link]();
if (myList[i] < min)
min = myList[i];
}
[Link]("Maximum number is " +min);
}
}
Checkpoint 3
01
Consider the following array :
double [] temperature = {33.4, 29.5, 35.0, 27.4, 40.0};
Construct a program segment to search for
temperature 29.5 in the array. Display the index if the
temperature is found, otherwise display message
“Temperature not found!”.
Checkpoint 3
02
Construct a program segment to search for
temperature entered by user in the array. Display the
index if the element is found, otherwise display
message “Temperature not found!”.
Checkpoint 3
03
Construct a program segment to search for
temperature that greater 25.0. Display the index if the
element is found, otherwise display message
“Temperature not found!”.
Checkpoint 3 04
Given the following code segment, write Java code to read numbers into
the array named numbers and calculate their average.
Scanner in = new Scanner([Link]);
double[] numbers = new double[10], sum = 0, average;
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
[Link](“The average is ” + average);
4.6 JAVA PROGRAM
Learning Outcome
At the end of lesson, students should be able to :
b) Write a programs that perform one dimensional array
operations for problems involving average, maximum
and minimum. (P)
- Do in practical session -