You are on page 1of 28

3.

4 ARRAY
(a) Define array and relate with memory allocation
3.4 Arrays

Learning Outcomes
At the end of this topic, you should be able to:
Define array and relate with memory allocation
Explain accessing arrays – initialize, input,
process and output.
Construct programs that perform one-
dimensional array operators for simple statistical
functions (minimum, maximum, frequency, total
and mean) and linear search.
a) Define array and relate with memory allocation
An array is a data structure that contains a group of elements.
Typically these elements are all of the same data type, such
as an integer, double or string. Arrays are commonly used in
computer programs to organize data so that a related set of
values can be easily sorted or searched.
An array is an ordered sequence of data of the same type.

For example :
marks 88.5 37.6 98.5 57.4 60.5

names “Amni” “Amin” “Iman” “Naim”

An array is a fixed-size collection of consecutive memory


locations.
Each memory location in an array is accessed by a relative
address called index.
An array consists of its :
■ Data type
■ Size
■ Index
■ Value
Array
For example : Data type name Array elements

double [ ] marks = {88.5, 37.6, 98.5, 57.4, 60.5};


value
Memory
location
[0] [1] [2] [3] [4] index

The size of array named marks is 5 from index 0 to index 4.


The value for the first element (index 0) of the array named marks is 88.5
The value for the second element (index 1) of the array named marks is 37.5
and so on.
REMEMBER…
arrays start
at index 0
What is the importance of array?

– To store multiple value of the same type


using single variable.
– Enable you to write programs that
manipulate larger amounts of data.
3.4 ARRAY
(b) Explain accessing arrays
– initialize, input, process and output.
3.4 Arrays

Learning Outcomes
At the end of this topic, you should be able to:
Define array and relate with memory allocation
Explain accessing arrays – initialize, input,
process and output.
Construct programs that perform one-
dimensional array operators for simple statistical
functions (minimum, maximum, frequency, total
and mean) and linear search.
CREATING ARRAYS

An array is an sequence of values; the values


in the array are called elements.
You can make an array of ints, doubles, or
any other data type, but all values in an array
must have the same data type.
To create an array, you must declare a
variable with an array type, and then create
the array itself.
CREATING ARRAYS
■ Array types look like other Java types, except
they are followed by square brackets ([ ])

DECLARE A VARIABLE
WITH BASIC TYPE WITH AN ARRAY TYPE
int counts; int [ ] counts;
 single variable  single variable
 single data type  single data type
 for individual value  for multiple value
CREATING ARRAYS

DECLARE A VARIABLE WITH AN ARRAY TYPE


double[ ] values; int[ ] counts;
 variable : values  variable : counts
 data type : double array  data type : integer array
CREATING THE ARRAY
values = new double [ 7 ]; counts = new int [ 5 ];
 makes values refer to an makes counts refer to an array
array of double, where the of int, where the number of
number of elements in elements in counts is five.
values is seven.
CREATING ARRAYS

■ You also can declare the variable and create the


array in a single line of code:
DOUBLE LINE OF CODE
double [ ]values; int [ ]counts;
values = new double[7]; counts = new int[5];

SINGLE LINE OF CODE


double [ ]values= new double[7]; int [ ]counts = new int[5];
CREATING ARRAYS
■ You can use any integer expression for the size of an
array, as long as the value is positive or zero element.

DECLARE A VARIABLE AND CREATE ARRAY USING VALID ELEMENTS


double [ ] values; int [ ] counts;
values = new double [7]; counts = new int[5];

double [ ] values = new double[7]; int [ ] counts = new int[0];

DECLARE A VARIABLE AND CREATE ARRAY USING INVALID ELEMENTS


double [ ] values; int [ ]counts = new int[-8];
values = new double[-3];
ACCESSING ARRAYS - initialize
■ When you create an array of ints, the elements are
initialized to zero by default.

Declare a variable int[ ]counts;


counts = new int[5];
Create array
All elements of the counts
counts 0 0 0 0 0
array initialized to zero 0 1 2 3 4
ACCESSING ARRAYS - initialize
■ You also has an option to initialize the value of
elements by yourself. For example :

Declare a counts array and


makes it refer to an array int[ ]counts = {1,2,-3,4,5}
of five elements
All elements of the counts
array equal to the value counts 1 2 -3 4 5
0 1 2 3 4
you initialize above
ACCESSING ARRAYS - initialize

Large number inside the box


(red color) are the elements of counts 0 0 0 0 0
0 1 2 3 4
the array.
Small numbers outside the
boxes are the indexes (or
indices) used to identify each counts 0 0 0 0 0
location in the array. 0 1 2 3 4

Notice that the index of the


first element is 0, not 1.
ACCESSING ARRAYS - process
The [ ] operator selects elements from an array:
System.out.println (“The zeroth element is “ + counts[0]

You can use the [ ] operator anywhere in an expression:


ASSIGNMENT STATEMENT STATE DIAGRAM AFTER ASSIGNMENT
STATEMENT
counts[0] = 7;
counts 7 0 0 0 0
0 1 2 3 4

counts[1] = counts[0] *2;


counts 7 14 0 0 0
0 1 2 3 4

counts[2]++; 7 14 1 0 0
counts
0 1 2 3 4

counts[0] = counts[1] + counts[4];


counts 14 14 2 0 0
0 1 2 3 4
ACCESSING ARRAYS - process
You can use any expression as an index, as long as it has
type int.
One of the common ways to index an array is with loop
variable. For example:
int[ ]counts = new int[5];
int i = 0;
while ( i < 5) { //while loop counts from 0 up to 5. When i is 5,
// the condition fails and the loop terminates.
System.out.println (counts[ i ]);
// executed when i is 0, 1, 2, 3 and 4
i++;
}
RECALL PREVIOUS KNOWLEDGE!

Change source code below using for loop.


int[ ]counts = new int[5];
int i =0;
while ( i < 5) { //while loop counts from 0 up to 5. When i is 5,
// the condition fails and the loop terminates.
System.out.println (counts[ i ]);
// executed when i is 0, 1, 2, 3 and 4
i++;
}
ACCESSING ARRAYS - process

int [ ] counts = new int [ 5 ];

For the counts array, the only legal indexes are


0, 1, 2, 3 and 4.
If the index is negative or greater than 4, the
result is an ArrayIndexOutOfBoundException.
Can you explain why?
ACCESSING ARRAYS – output/display
■ You can use println to display an element of the array.
For example :
ASSIGNMENT STATEMENT OUTPUT
int [ ] counts = new int [ ];
counts 0 0 0 0 0
0 1 2 3 4

System.out.println (counts[ 0 ]); Output : 0


counts[0] = 7;
counts 7 0 0 0 0
0 1 2 3 4

System.out.println (counts[ 0 ]); Output : 7


counts[1] = counts[0] *2;
counts 7 14 0 0 0
0 1 2 3 4
System.out.println (counts[ 0 ]); Output : 7
ACCESSING ARRAYS – output/display
■ You can use println to display an element of the array.
Complete the table below.
ASSIGNMENT STATEMENT OUTPUT
counts[0] = counts[1] ;
counts 14 14 0 0 0
0 1 2 3 4

System.out.println (counts[ 0 ]); Output : ______


counts[0] ++;
counts
0 1 2 3 4

System.out.println (counts[ 0 ]); Output : _______


int [ ] counts = { 1, 2, -3, 4, 5};
counts
0 1 2 3 4

System.out.println (counts[ 0 ]); Output : _______


ACCESSING ARRAYS – output/display
■ You can use println to display an element of the array.
Complete the table below.
ASSIGNMENT STATEMENT OUTPUT

counts[0] = counts[1] ; counts


0 1 2 3 4

System.out.println (counts[ 1 ]); Output : _______

System.out.println (counts[ 2 ]); Output : _______

System.out.println (counts[ 3 ]); Output : _______


System.out.println (counts[1]+counts[2]); Output : _______

System.out.println (counts[3]+counts[4]); Output : _______


ACCESSING ARRAYS – output/display

■ Just now you learnt how to display an element of the array.


■ How to display ALL elements of the array?
■ Can you use the same syntax?
■ Now look at the example below:
int[ ]counts = {1,2,-3,4,5};
System.out.println (counts);
■ Unfortunately, the output is something like: [I@bf3f7e0
■ If we want to display ALL elements of the array, we need to
use a looping
ACCESSING ARRAYS – output/display

■ If we want to display ALL elements of the array, we


can use a while loop as example below.
int[] counts = {1,2,-3,4,5};
int counter = 0;
while (counter < counts.length) {
System.out.println(“Counts "+ counts[counter]);
counter=counter+1;
}
ACCESSING ARRAYS – output/display
■ If we want to display ALL elements of the array, we
can use a for loop as example below.

class PrintArray{
public static void main(String[] args){
int[] arr = {1,2,3,4,5,6,7};
for (int i=0;i<arr.length;i++) {
System.out.print(arr[i]);
}
}
}

You might also like