OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 3 : Topic - 1 : Arrays
1. Introduction to Arrays
Definition
An array in Java is a collection of variables of the same data type stored at contiguous memory locations. It allows us to store multiple values in a single variable, instead of declaring separate variables for each value.
-
Arrays are fixed-size (the size must be defined at the time of creation).
-
Each element in an array is accessed using an index.
-
Array indexing in Java starts from 0.
Why Arrays?
-
To handle large amounts of data efficiently.
-
Easy traversal using loops.
-
Better performance as elements are stored in continuous memory.
Key features of Arrays
Store Primitives and Objects: Java arrays can hold both primitive types (like int, char, boolean, etc.) and objects (like String, Integer, etc.)
Contiguous Memory Allocation When we use arrays of primitive types, the elements are stored in contiguous locations. For non primitive types, references of items are stored at contiguous locations.
Zero-based Indexing: The first element of the array is at index 0.
Fixed Length: After creating an array, its size is fixed; we can not change it.
Example (Without Array)
➡️ Problem: Repetitive code, difficult to manage large data.
Example (With Array)
2. Declaration and Initialization of Arrays
Array Declaration
In Java, arrays are objects, so they must be declared and created using the new
keyword.
Syntax
Array Creation
Example
Array Initialization
-
Method 1: Static Initialization (Direct values)
-
Method 2: Dynamic Initialization (Assign values later)
Complete Example
3. Storage of Array in Computer Memory
How Arrays Are Stored in Memory
-
Contiguous Memory Allocation
-
All array elements are stored next to each other in a continuous memory block.
-
This allows fast access using index.
-
-
Array Indexing
-
Array index starts at 0.
-
Address of
arr[i]
is calculated as:
-
-
In Java (Special Case)
-
Arrays are objects in Java.
-
When we create an array using
new
, memory is allocated on the heap. -
The variable (array name) stores a reference (address) to the memory location.
-
Example:
Here,
arr
holds a reference (address) pointing to 5 contiguous memory locations in the heap.
-
Diagram Representation
-
Suppose
arr[0]
starts at address1000
and eachint
takes4 bytes
:-
arr[0]
→ Address 1000 -
arr[1]
→ Address 1004 -
arr[2]
→ Address 1008 -
arr[3]
→ Address 1012 -
arr[4]
→ Address 1016
-
Example Program to Show Memory Reference
Output (example):
-
[I
→ Integer Array -
@15db9742
→ Hashcode representing memory location
4. Accessing Array Elements
Definition
Array elements in Java are accessed using their index number.
-
Indexing starts from 0 and ends at length - 1.
-
Accessing beyond the last index causes
ArrayIndexOutOfBoundsException
.
Syntax
Example
Output:
5. Operations on Array Elements
Array elements can be used in operations like update, traverse, search, sort, and aggregate (sum/average).
1. Traversal
Visiting each element of the array.
2. Updating Elements
Changing the value at a specific index.
3. Searching
Find a specific element by checking each value.
4. Sorting
Rearranging elements (ascending or descending).
(Using Arrays.sort()
from java.util
).
5. Aggregate Operations (Sum, Average)
6. Assigning Array to Another Array
In Java, arrays are objects. When you assign one array to another, only the reference (address) is copied, not the actual elements.
Example: Reference Assignment
Output:
➡️ Both arr1
and arr2
point to the same memory location.
Creating a Copy of Array (Cloning)
To avoid reference sharing, use clone() or Arrays.copyOf()
.
Method 1: Using clone()
Method 2: Using Arrays.copyOf()
Method 3: Manual Copy
Comments
Post a Comment