Arrays and Methods
In programming, we often deal with multiple but related information, like quiz scores.
They are all numeric and are all categorized as individual values. If we have five quiz
scores, then we have to store the score values to different variables just to compute
their average, which is very impractical and tedious.
This is what arrays are for. Instead of having to separately store related information in
different variables (named memory locations), you can store them as a collection in just
one variable or array.
A method is a block of code which only runs when it is called.
You can pass data, known as parameters, into a method.
Methods are used to perform certain actions, and they are also known as functions.
LESSON 5 – Arrays and Methods
At the end of this lesson, you should be able to;
Use array declaration
Use array in a Java programming
Construct a method
Call a method in Java programming
5.1 Java Arrays
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 [ ]JarrayValue;
Initializing Arrays
Below is the syntax for creating an array of integers
int arrayNumbers [ ];
arrayNumbers = new int [5] ;
This indicates that arrayNumbers has a length of 5, meaning the array is capable
of holding or storing 5 integers.
Below is the syntax for assigning values to each element of f arrayNumbers.
arrayNumber[0] = 10; // initialize the first element
arrayNumber[1]= 120; // initialize the second element
arrayNumber[2]= 30; // initialize the third element
arrayNumber[3]= 80; // initialize the fourth element
arrayNumber[4]= 100; // initialize the fifth element
The array index will always start with index of zero [0]. In this case, the elements of
array Numbers are 10, 120, 30, 80, and 100.
1
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 of values provided between braces
and separated by commas.
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.
Example program 1
Output
2
5.2 Methods
A method must be declared within a class. It is defined with the name of the method,
followed by parentheses (). Java provides some pre-defined methods, such
as System.out.println(), but you can also create your own methods to perform certain
actions:
Create a method inside the main:
Example Explained
myMethod() is the name of the method
static means that the method belongs to the Main class and not an object of the
Main class. You will learn more about objects and how to access methods through
objects later in this tutorial.
void means that this method does not have a return value. You will learn more
about return values later in this chapter
Call a Method
To call a method in Java, write the method's name followed by two parentheses () and a
semicolon;
In the following example, myMethod() is used to print a text (the action), when it is
called:
3
Output
The public keyword is an access modifier used for classes, attributes, methods and
constructors, making them accessible by any other class.
References:
Book:
Manansala, E. T.(2017)C++ and Java Programming Made Simple, The Library
Publishing House Inc.
Internet Links: