📘LESSON 4
3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Define arrays in Java.
- Declare and initialize arrays correctly.
- Access and modify array elements.
- Use loops to process array data.
- Explain the importance of arrays in Java programming.
3.2 Overview
Arrays are data structures used to store multiple values of the same data type in a single variable. Instead of creating many individual variables, arrays allow developers to organize and process collections of related data efficiently.
This lesson introduces learners to Java arrays, including array declaration, initialization, indexing, and iteration. Learners will also explore how arrays simplify data management in software applications.
Arrays are commonly used in:
- banking systems,
- inventory systems,
- student management systems,
- games,
- and enterprise applications.
Understanding arrays is essential because arrays form the foundation for advanced data structures and collections in Java programming.
KT0401 — Introduction to Arrays
An array is a collection of elements of the same data type stored in contiguous memory locations.
Each value inside an array is called an element.
Every element in an array has an index position.
Array indexing starts at 0.
Example of an Array
int[] numbers = {10, 20, 30, 40};
Explanation
| Component | Description |
|---|---|
| int[] | Array data type |
| numbers | Array variable name |
| {} | Stores array elements |
| 10,20,30,40 | Array values |
Importance of Arrays
Arrays help developers:
- store multiple values efficiently,
- simplify repetitive operations,
- improve data organization,
- and process grouped information.
KT0402 — Declaring Arrays
Array declaration defines the array type and variable name.
Syntax
dataType[] arrayName;
Example
int[] marks;
This creates an integer array variable called marks.
Different Ways to Declare Arrays
int[] numbers;
String[] names;
double[] prices;
KT0403 — Initializing Arrays
Array initialization assigns values to an array.
Arrays may be initialized during declaration or later in the program.
Array Initialization During Declaration
int[] numbers = {1, 2, 3, 4, 5};
Array Initialization Using new Keyword
int[] numbers = new int[5];
This creates an array with five elements.
Assigning Values Individually
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
KT0404 — Accessing Array Elements
Array elements are accessed using indexes.
Indexes begin at 0.
Example
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 20, 30};
System.out.println(numbers[0]);
System.out.println(numbers[1]);
System.out.println(numbers[2]);
}
}
Output
10
20
30
Modifying Array Elements
Array values can be updated using indexes.
Example
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 20, 30};
numbers[1] = 50;
System.out.println(numbers[1]);
}
}
Output
50
KT0405 — Looping Through Arrays
Loops are commonly used to process array elements.
The for loop is frequently used with arrays.
Example Using a for Loop
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40};
for(int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
Output
10
20
30
40
Enhanced for Loop
Java also supports the enhanced for loop.
Example
public class Main {
public static void main(String[] args) {
int[] numbers = {5, 10, 15};
for(int number : numbers) {
System.out.println(number);
}
}
}
Output
5
10
15
KT0406 — Array Length
The length property returns the number of elements in an array.
Example
public class Main {
public static void main(String[] args) {
int[] marks = {70, 80, 90};
System.out.println(marks.length);
}
}
Output
3
Importance of Array Length
The length property helps:
- control loops,
- avoid errors,
- and process arrays dynamically.
KT0407 — Common Array Errors
One common array error is:
- ArrayIndexOutOfBoundsException
This occurs when accessing an invalid index.
Example of an Error
int[] numbers = {1, 2, 3};
System.out.println(numbers[5]);
This causes an error because index 5 does not exist.
Preventing Array Errors
Developers should:
- check array length,
- use proper loop conditions,
- and avoid invalid indexes.
3.5 Key Notes / Summary
- Arrays store multiple values of the same data type.
- Array indexes start at 0.
- Arrays can be declared and initialized in different ways.
- Array elements are accessed using indexes.
- Loops are commonly used to process arrays.
- The length property returns the number of elements.
- Invalid indexes can cause array errors.