Understanding Arrays in Programming
Array Definition and Access
Container Overview
Consider a scenario where we need to analyze employee salary data within a company, such as calculating average salaries or identifying the highest salary. With 50 employees, manually declaring 50 individual variables becomes impractical and error-prone. Containers offer a solution by allowing us to store all data in one place for unified operations.
A container is a structured storage mechanism that holds multiple elements of the same data type.
1.1 Array Concept
An array is a fixed-size container designed to hold elements of a single data type.
Array Declaration Methods:
Method One
DataType[] arrayName = new DataType[length];
Method Two
DataType[] arrayName = new DataType[]{element1, element2, element3, ...};
Method Three
DataType[] arrayName = {element1, element2, element3, ...};
Arrays have a fixed langth that cannot be changed once set, similar to a water bottle with a specific capacity.
Example:
int[] numbers = new int[3];
1.2 Array Access
Each element in an array has an index starting from zero. Elements can be accessed using arrayName[index].
The property arrayName.length returns the number of elements in the array.
Accessing and modifying elements:
int[] values = {1, 2, 3, 4, 5};
values[0] = 6;
int retrievedValue = values[0];
System.out.println(values[0]);
Memory Representation of Arrays
2.1 Memory Basics
Memory serves as temporary storage for running programs. Code stored on disk cannot execute until loaded into memory.
2.2 JVM Memory Segmentation
Different memory areas are allocated to optimize processing efficiency.
2.3 Array Storage in Memory
When creating an array, it's stored in heap memory, and the variable stores its reference address.
int[] arr = new int[3];
System.out.println(arr); // Outputs something like [I@5f150435
Two arrays sharing the same memory reference:
int[] firstArray = new int[3];
int[] secondArray = firstArray;
secondArray[1] = 9;
System.out.println(firstArray[1]); // Prints 9
Common Array Operations
3.1 Array Index Out of Bounds Exception
Attempting to access an index beyond the array bounds results in an exception.
int[] arr = {1, 2, 3};
System.out.println(arr[3]); // Throws ArrayIndexOutOfBoundsException
3.2 Null Pointer Exception
Assigning null to an array variable prevents further access.
int[] arr = {1, 2, 3};
arr = null;
System.out.println(arr[0]); // Throws NullPointerException
3.3 Array Traversal
Iterating through each element of an array:
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
3.4 Finding Maximum Value
To determine the largest element:
int[] arr = {5, 15, 2000, 10000, 100, 4000};
int maximum = arr[0];
for (int i = 0; i < arr.length; i++) {
if (arr[i] > maximum) {
maximum = arr[i];
}
}
System.out.println("Maximum value is: " + maximum);
3.5 Array Reversal
Reversing the order of elements in an array:
int[] arr = {1, 2, 3, 4, 5};
for (int left = 0, right = arr.length - 1; left <= right; left++, right--) {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
Arrays as Method Parameters and Return Values
4.1 Arrays as Parameters
Arrays passed to methods are passed by reference.
public static void main(String[] args) {
int[] nums = {1, 3, 5, 7, 9};
printArray(nums);
}
public static void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
4.2 Arrays as Return Values
Methods can return array references.
public static void main(String[] args) {
int[] result = getArray();
for (int i = 0; i < result.length; i++) {
System.out.println(result[i]);
}
}
public static int[] getArray() {
int[] arr = {1, 3, 5, 7, 9};
return arr;
}
4.3 Parameter Type Differences
Basic types pass values; reference types pass addresses.
Example with primitive types:
public static void main(String[] args) {
int a = 1;
int b = 2;
change(a, b);
System.out.println(a); // Still 1
System.out.println(b); // Still 2
}
public static void change(int a, int b) {
a = a + b;
b = b + a;
}
Example with array reference:
public static void main(String[] args) {
int[] arr = {1, 3, 5};
change(arr);
System.out.println(arr[0]); // Now prints 200
}
public static void change(int[] arr) {
arr[0] = 200;
}