An array is a data structure that stores a collection of elements, typically of the same type, in a contiguous block of memory. It allows you to store multiple values in a single variable, making it easier to manage and access them efficiently.
Key Characteristics of an Array:
1. Fixed Size: Once an array is created, its size is fixed and cannot be changed (in most languages)
2. Indexed: Elements in an array are stored at specific positions, called indices, which usually start from 0.
3. Homogeneous Elements: All elements in an array must be of the same type (e.g., all integers, all floats, or all strings).
4. Efficient Access: Arrays allow constant-time access to elements via their index.
Example in Different Programming Languages:
Python (using a list as an array substitute):
arr = [1, 2, 3, 4, 5]
print(arr[0]) # Access the first element
Java:
int[] arr = {1, 2, 3, 4, 5};
System.out.println(arr[0]); // Access the first element
Arrays are commonly used in various algorithms for sorting, searching, and organizing data.