ARRAYS (FOUNDATION)
1. Why Do We Need Arrays?
Consider the following program:
int a, b, c, d;
a = [Link]();
b = [Link]();
c = [Link]();
d = [Link]();
This works when the number of values is small.
However, suppose we need to store 100 values. Then we would need to write something like:
int a1, a2, a3, ... , a100;
Problems with this approach:
● We must create many different variables
● Each variable requires memory to store the value
● Each variable also has its own memory reference (address)
Therefore memory usage becomes:
Memory = memory for storing data + memory for storing many references
Managing and processing such a large number of variables becomes inefficient.
Arrays solve this problem by allowing us to store many values using a single variable name.
Example:
int[] arr = new int[100];
Here:
● arr stores the reference to the array
● All elements are stored contiguously in memory
● Individual elements are accessed using indices
2. Array Definition
An array is a data structure that stores a fixed number of elements of the same data type in
contiguous memory locations.
Example:
int[] arr = new int[5];
This creates an array capable of storing 5 integers.
3. Array Indexing
Each element in an array is accessed using an index.
Example representation:
Index 0 1 2 3 4
Important points:
● Indexing starts from 0
● The last index of an array is N − 1
Example:
If size = 5
First index = 0
Last index = 4
4. Array Length
The length of an array can be obtained using the length property.
Example:
int[] arr = new int[10];
[Link]([Link]);
Output:
10
[Link] returns the size of the array.
5. Default Initialization
When an integer array is created, its elements are automatically initialized to 0.
Example:
int[] arr = new int[5];
[Link](arr[0]);
Output:
Default values depend on the data type:
Data Type Default Value
int 0
double 0.0
boolean false
object null
6. Array Index Error
Accessing an index outside the valid range causes an exception.
Example:
int[] arr = new int[3];
[Link](arr[3]);
Error:
ArrayIndexOutOfBoundsException
Valid indices for this array are:
0, 1, 2
7. Taking Input in an Array
Manual approach:
arr[0] = [Link]();
arr[1] = [Link]();
arr[2] = [Link]();
Using loops (recommended):
for(int i = 0; i < n; i++){
arr[i] = [Link]();
}
This allows input for any number of elements.
8. Sum of Elements in an Array
Problem: Find the sum of all elements.
Approach:
1. Initialize a variable sum with 0
2. Traverse the array using a loop
3. Add each element to sum
Code:
int sum = 0;
for(int i = 0; i < n; i++){
sum += arr[i];
}
9. Frequency of an Element
Problem: Count how many times a number k appears in the array.
Example:
3 6 7 6 11 6 14
k=6
Approach:
Traverse the array and check each element.
if(arr[i] == k){
count++;
}
10. Maximum Element in an Array
Goal: Find the largest element.
Incorrect initialization:
max = 0
This fails when all elements are negative.
Example:
-8 -4 -3 -5
Correct approach:
int max = Integer.MIN_VALUE;
for(int i = 0; i < n; i++){
if(arr[i] > max){
max = arr[i];
}
}
Integer.MIN_VALUE ensures initialization does not affect the result.
Key Concepts Summary
● Arrays store multiple values using one variable
● Elements are stored in contiguous memory
● Indexing starts from 0
● Last index = N − 1
● Array size is obtained using [Link]
Homework Problems
1. Find the minimum element in an array
2. Count the number of even elements
3. Check whether a given element exists in the array