Lesson 6: </
JAV
A
UI
java >
ARRAYS U
X
Lesson objectives:
use array declaration;
1
use arrays in a Java
understand accessing
4 program.
2 elements of an array;
understand
3 multidimensional
arrays; and
ARRAY
An array is like a container that holds
a fixed number of values of a
particular type. Memory space is
allocated for values of similar type
during array declaration. The length of
the array is specified and remains
constant at the time it was created.
Advantages of using
array
• In a large group of elements, a
particular element can be accessed
easily using the array name and the
element index.
• Arrays are useful in executing
calculations within a loop.
Declaring an array
Below is the array syntax:
<data type> [ ] <array name>
Example:
Int [ ] arrayNumbers;
String [ ] arrayNames;
char [ ] arrayLetters;
double [ ] arrayDouble;
float [ ] arrayValue;
We can also write it this way:
int arrayNumbers [ ];
Initializing arrays
Below is the syntax for creating
an array of integers
arrayNumbers = new int [ 5 ];
This indicates the arrayNumbers
has a length of 5, meaning the
array is capable of holding or
storing 5 integers.
Initializing arrays
Below is the syntax for assigning values to
each element of arrayNumbers.
arrayNumbers [0] = 10; //initialize the first
element
arrayNumbers [1] = 120; //initialize the second
element
arrayNumbers [2] = 30; //initialize the 3rd
element
arrayNumbers [3] = 80; //initialize the 4th
element
arrayNumbers [4] = 100; //initialize the 5th
Initializing arrays
indices
first index
0 1 2 3 4
10 120 30 80 100
Array length is 5
elements
Initializing arrays
An array uses indexing to store and retrieve its
elements. Each item in an array is called element.
Hence, if we want to display the 4th element of the
array, we write:
System.out.println(arrayNumbers [3]);
The 4th element is at index 3 because the indexing
begins with zero. Another way of initializing an
array;
int [ ] arrayNumbers = {10, 120, 30, 80, 100};
The length of the array is determined by the number
Initializing arrays
Example:
String [ ] arrayNames = {“Ethel”,”Louise”,”Joy”};
char [ ] arrayLetter = {‘a’, ‘b’, ‘c’, ‘d’};
To declare and create an array at the same time, we
write:
int [ ] arrayNumbers = new int [5];
Always remember that array index begins with zero,
so if the length of the array is 5, the last index is 4.
Sample program 1
Sample program 2
We use the for statement to
call the elements from
arrayNames, since the index
number is not constant. We
assign i variable to its index
number, so it will call index 0
to index 4.
Sample program 3
Sample program 4
TECH notes
• An array is like a container that holds a fixed number of
values of a particular type.
• Each item in an array is called an element.
• We use the for statement to call the elements from
arrayNames.
• The array index will always start with index of zero [0].
THANK
S!
Do you have any questions?
addyouremail@freepik.co
m +91 620 421 838
yourcompany.com
CREDITS: This presentation template was
created by Slidesgo, including icons by
Flaticon, and infographics & images by
Freepik
Please keep this slide for attribution