Array
Introduction Array
Array is the collection of similar datatype or simlar element of object in data structure
An array is defined as a fixed-size collection of elements of the same data type stored in contiguous memory locations. It is the simplest data structure where each element of the array can be accessed by using its index.
Properties of arrays
Each element of the array is of the same data type and same size. For example: For an array of integers with the int data type, each element of the array will occupy 4 bytes.
Elements of the array are stored in contiguous memory locations. For example : 200 is the starting address (base address) assigned to the first element of the array and each element of the array is of integer data type occupying 4 bytes in memory.
Array
Where can arrays be used ?
- Arrays should be used where the number of elements to be stored is already known.
- Arrays are commonly used in computer programs to organize data so that a related set of values can be easily sorted or searched.
- Arrays work well when we have to organize data in multidimensional format. We can declare arrays of as many dimensions as we want.
- If the index of the element to be modified is known beforehand, it can be efficiently modified using arrays due to quick access time and mutability.
Array Advantage
- Code Optimization: It makes the code optimized, we can retrieve or sort the data efficiently.
- Random access: We can get any data located at an index position.
Disadvantages of arrays
- Since arrays are fixed-size data structures you cannot dynamically alter their sizes. It creates a problem when the number of elements the array is going to store is not known beforehand.
- If more memory is allocated than required, it leads to the wastage of memory space and less allocation of memory also leads to a problem.
- Insertion and Deletion in arrays are difficult and costly since the elements are stored in contiguous memory locations, hence, we need to shift the elements to create/delete space for elements.
Types of Array
- 1. Single Dimensional Array
- 2.Multidimensional Array
1 Single Dimensional Array.
Syntax to Declare an Array in Java
Instantiation of an Array in Java
Array Question
Next Topic
No comments:
Post a Comment