Introduction to Array
Table of Contents + β
Arrays are one of the most fundamental data structures in computer science and programming. And Arrays are available in almost all programming languages because of their simplicity and efficiency.
π What is an Array?
An array is used to store elements of the same data type in a contiguous block of memory. Then each element in the array can be accessed easily using its index since all the elements are stored in a contiguous block of memory.
π Array Characteristics
Array has the following characteristics:
- Same Data Type: All elements in the array must be of the same data type (e.g., integers, strings, etc.).
- Contiguous Memory: Elements are stored in contiguous memory locations. (Contiguous means βnext to each otherβ in memory)
- Indexed Access: Since elements are stored in contiguous memory locations, we can access any element directly using its index.
- Fixed Size: The size of the array is fixed at the time of creation since memory is allocated for the array based on its size in contiguous memory. And Computer will not know whether the next memory location is free or not to allocate memory for new elements. So it cannot be resized dynamically.
How array looks in memory?
Index: 0 1 2 3 4Value: [10, 20, 30, 40, 50]Address: 100, 104, 108, 112, 116In the above example:
- The array has 5 elements with indexes ranging from 0 to 4.
- Each element in the array is of type integer.
- The first element (10) is stored at memory address 100, the second element (20) is stored at memory address 104, and so on.
- Each array element takes memory as per its data type (for int, it takes 4 bytes, thatβs why addresses starts with 100 and increments by 4).
π’ How to create an array in Java?
In Java, we can create an array using the following syntax:
dataType[] arrayName = new dataType[arraySize];Here, dataType is the type of elements that the array will store (e.g., int, float, String, etc.), arrayName is the name of the array, and arraySize is the number of elements that the array can hold.
For example, to create an array of integers with 5 elements, we can use the following code:
int[] numbers = new int[5];it will create an array named numbers that can hold 5 integers like
Index: 0 1 2 3 4Value: [0, 0, 0, 0, 0]By default, all elements in the array are initialized with default value of given data type (for int, default value is 0).
π₯ Inserting values into an array
In Java, we can insert values into an array using the index of the element we want to modify. For example:
int[] numbers = new int[5];numbers[0] = 10;numbers[1] = 20;numbers[2] = 30;numbers[3] = 40;numbers[4] = 50;π₯ How to initialize an array in Java?
If we know the values at the time of array creation, we can initialize the array like this:
int[] numbers = {10, 20, 30, 40, 50};This will create an array named numbers that will hold 5 integers with the specified values.
Index: 0 1 2 3 4Value: [10, 20, 30, 40, 50]Accessing array elements
We can access the elements of an array using their index. For example, to access the first element of the numbers array, we can use the following code:
int[] numbers = {10, 20, 30, 40, 50};System.out.println(numbers[0]); // Output: 10System.out.println(numbers[2]); // Output: 30How elements are accessed so fast?
When we access an element in an array using its index, the computer calculates the memory address of that element using a simple formula:
Address of element = Base address + (Index * Size of each element)Where:
- Base address: The memory address of the first element in the array.
- Index: The index of the element we want to access.
- Size of each element: The size of each element in the array (in bytes).
For example, if we have of 5 integers it stored in memory as follows:
Index: 0 1 2 3 4Value: [10, 20, 30, 40, 50]Address: 100, 104, 108, 112, 116To access the element at index 2 (which is 30), the computer will calculate the memory address as follows:
Address of element at index 2 = 100 + (2 * 4) = 108Then, the computer will go to memory address 108 and retrieve the value stored there, which is 30.
Time complexity of accessing array elements
Accessing an element in an array by its index takes constant time, O(1), because we can directly calculate the memory address of the element using its index.
π Array Operations and their Time Complexities
| Operation | Time Complexity | Description |
| Access | O(1) | Accessing an element by its index takes constant time. |
| Search | O(n) | Searching for an element requires checking each element in the worst case. |
| Insertion | O(n) | Inserting an element may require shifting elements, leading to linear time complexity. |
| Deletion | O(n) | Deleting an element may require shifting elements, leading to linear time complexity. |