3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Declare and initialize arrays.
- Insert and remove array elements.
- Iterate through arrays.
- Sort and search arrays.
- Use Java array utility methods.
3.2 Overview
Arrays are data structures used to store multiple values of the same data type in a single variable. Arrays are widely used in Java applications to organize and process collections of data.
This lesson introduces learners to practical array operations such as:
- declaration,
- initialization,
- iteration,
- sorting,
- searching,
- copying,
- and comparison.
Arrays are important in:
- data processing,
- calculations,
- collections,
- and software applications.
Understanding arrays is important because they form the foundation of many advanced data structures in programming.
PA0401 — Declare an Array Variable
An array variable stores multiple values of the same data type.
Example Array Declaration
Java
int[] numbers;
Java
String[] names;
Practical Activity
Learners must:
- declare arrays,
- use correct array syntax,
- and apply correct data types.
PA0402 — Instantiate an Array
Arrays must be instantiated before use.
Example Array Instantiation
Java
numbers = new int[5];
Java
names = new String[3];
Practical Activity
Learners must:
- create arrays,
- specify array sizes,
- and initialize arrays correctly.
PA0403 — Use Array Literals
Array literals allow values to be assigned directly.
Example Array Literal
Java
int[] nums = {1, 2, 3, 4, 5};
Java
String[] fruits = {“Apple”, “Banana”, “Orange”};
Practical Activity
Learners must:
- create arrays using literals,
- assign values correctly,
- and display array contents.
PA0404 — Iterate Arrays
Arrays can be processed using loops.
Example for Loop
Java
for (int i = 0; i < nums.length; i++) {
System.out.println(nums[i]);
}
Example Enhanced for Loop
Java
for (String fruit : fruits) {
System.out.println(fruit);
}
Practical Activity
Learners must:
- iterate through arrays,
- use loops correctly,
- and display array values.
PA0405 — Insert Elements into an Array
Array elements are inserted using indexes.
Example
Java
nums[0] = 10;
Java
fruits[1] = “Mango”;
Practical Activity
Learners must:
- insert values into arrays,
- update elements,
- and display updated arrays.
PA0406 — Remove Elements from an Array
Array elements may be removed by assigning null or default values.
Example
Java
fruits[1] = null;
Practical Activity
Learners must:
- remove array elements,
- update array contents,
- and display results.
PA0407 — Find Minimum and Maximum Values in an Array
Arrays can be processed to determine minimum and maximum values.
Example
Java
int min = nums[0];
int max = nums[0];
Practical Activity
Learners must:
- identify minimum values,
- identify maximum values,
- and display results.
PA0408 — Copy an Array
Arrays can be copied using built-in methods.
Example
Java
int[] numsCopy = nums.clone();
Practical Activity
Learners must:
- copy arrays,
- compare original and copied arrays,
- and display array contents.
PA0409 — Convert Arrays to Strings
Java provides utility methods for array display.
Example
Java
Arrays.toString(nums);
Practical Activity
Learners must:
- convert arrays to strings,
- display formatted output,
- and interpret results.
PA0410 — Sort Arrays
Arrays can be sorted using utility methods.
Example
Arrays.sort(nums);
Practical Activity
Learners must:
- sort arrays,
- display sorted arrays,
- and compare results.
PA0411 — Fill Arrays
Arrays can be filled with specific values.
Example
Java
Arrays.fill(nums, 5);
Practical Activity
Learners must:
- fill arrays,
- assign repeated values,
- and display results.
PA0412 — Search Arrays
Arrays can be searched using binary search methods.
Example
Java
Arrays.binarySearch(nums, 3);
Practical Activity
Learners must:
- search arrays,
- identify indexes,
- and display search results.
PA0413 — Check if Arrays are Equal
Arrays can be compared using equality methods.
Example
Java
Arrays.equals(nums, numsCopy);
Practical Activity
Learners must:
- compare arrays,
- determine equality,
- and display comparison results.
PA0414 — Construct an Array
Arrays can be constructed directly during declaration.
Example
Java
int[] scores = new int[]{10, 20, 30};
Practical Activity
Learners must:
- construct arrays,
- initialize arrays,
- and display values.
PA0415 — Initialise an Array
Arrays can be initialized during creation.
Example
Java
int[] marks = {80, 90, 75};
Practical Activity
Learners must:
- initialize arrays,
- assign starting values,
- and display contents.
3.5 Key Notes / Summary
- Arrays store multiple values of the same data type.
- Arrays use indexes to access elements.
- Arrays can be iterated using loops.
- Java provides utility methods for sorting and searching arrays.
- Arrays can be copied and compared.
- Arrays are foundational data structures in Java programming.