3.4 Array 0903
3.4 Array 0903
LECTURE 1 W8 2020/2021
QUOTE OF THE WEEK
marks=97.5; marks={97.5,41.8,36.5,100,75.3,57.8,67.5};
One value assign to variable Multiple values assign to array marks
marks Values of array marks known as element
Introduction to Array
An array is an ordered sequence of data of the same type.
ages 41 37 56 28 42
1.
int [ ] mark = new int [7];
Label the components of the array declaration above.
Array name:
Data type:
Size:
2. Declare and create an array to store the weights of your five friends.
3.4 Array
Learning Outcomes
At the end of this topic, you should be able to:
a) Define array and relate with memory allocation
b) Explain accessing arrays – initialize, input, process and output.
3. Initialize array value
■ When you create an array of ints, the elements are initialized to
zero by default.
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
counts[2]++;
counts 7 14 1 0 0
0 1 2 3 4
OR
OR
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]);
}
}
}
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]);
}
}
}