Java Arrays: Dimensions, Indexing, and Memory Management
Array Indexing and Access
To retrieve an element from an array, you use an
indexorsubscript.The
getmethod (or direct array accessarray[I]) allows retrieval of an element at a specific index I.Arrays are zero-indexed, meaning the first element is at index 0, and the last element of an array with N elements is at index N-1. For example, an array of 10 elements requires using index 9 for the last element.
Array Dimensions
Arrays can have multiple dimensions to represent complex data structures.
One-Dimensional (1D) Array: A simple list of elements.
Example:
[element0, element1, element2, element3].Location is identified by a single index, e.g.,
location(I).
Two-Dimensional (2D) Array: Represents a table or a grid (rows and columns).
Example: A classroom layout.
The first index represents the
rowand the second represents thecolumn.If an array element is at
(2,3):The first index (row) is 2, meaning it's the third row (since 0 is the first row).
The second index (column) is 3, meaning it's the fourth column (since 0 is the first column).
For 'Hassan' on the 'third row' (index 3) and 'fourth column' (index 3), the location would be
(3,3).
Three-Dimensional (3D) Array: Extends 2D to include another layer, such as rooms on a floor.
Example: A specific student's location in a classroom on a particular floor.
Indices would be
(room_number, row_number, column_number).
Multi-Dimensional Arrays (Beyond 3D):
The concept can be extended to
Ndimensions.Example for a 5-dimensional array to find a student's location in a large institution:
location(block_number, floor_number, room_number, row_number, column_number).The discussion further implies an even higher dimension if considering multiple campuses, e.g.,
campus_numberas the highest dimension, making it potentially a 6-dimensional array.
Practical vs. Theoretical Limits of Array Dimensions
Theoretical Limit: Theoretically, arrays can be created with an
Nnumber of dimensions. The exact theoretical limit can be very large, symbolised asPANDAMXin the discussion.Practical Limit: Arrays are stored in Random Access Memory (RAM), which is a finite resource (
limited RAM). This imposes a practical constraint on the size and number of dimensions an array can have.Virtual Memory: Modern operating systems address RAM limitations through
virtual memoryalgorithms.Virtual memory uses external storage (like Hard Disk Drives (HDDs) or Solid State Drives (SSDs)) to temporarily store data that would otherwise reside in RAM.
When a program requires more RAM than physically available, the operating system maps portions of the SSD/HDD as virtual RAM.
This capability makes the perceived 'limit of RAM' virtually 'unlimited' for advanced operating systems, allowing for the creation and management of very large and high-dimensional arrays.
Array Indexing Rules
The index or subscript of an array must always be non-negative.
The lowest valid index is 0.
The highest valid index is
number of elements - 1(N-1).
Arrays as Reference Data Types and Default Values
In Java, arrays are
reference data types.Upon creation, each element of an array is automatically initialized with a
default valuedetermined by its data type. For instance,intarray elements default to 0,booleantofalse, andobjecttypes tonull.
Fixed Size of Arrays in Java
Arrays in Java (and also in C#) are of a
fixed sizeonce they are instantiated.You cannot directly add a 13^{th} element to an array initially created for 12 elements.
If a change in array size is necessary (e.g., to increase or decrease capacity), the typical approach is to create a new array of the desired size and then copy the elements from the original array to the new one.
Dynamic Argument Handling with Arrays
When designing a method that needs to accept a
variable number of arguments(e.g., 2, 3, 4, 5 or more), you can specify the parameter as anarray type.This allows the method to receive and process
nnumber of arguments by treating them as elements within the passed array.In C#, this concept is also used for passing
command-line parametersto a program.