Basics of Arrays

An array is a linear data structure that stores multiple elements of the same type in contiguous memory locations. Arrays allow efficient indexed access to their elements.

Key Properties of Arrays:

Array Representation:

Arrays are visualized as buckets or boxes storing elements, each with an index starting from 0. For example, an array of 5 elements has indices from 0 to 4.

Common Operations on Arrays:

Example in Python:

my_array = [10, 20, 30, 40, 50]
print(my_array[0])  # Outputs: 10

# Traversing to print all elements
for element in my_array:
    print(element)

Insertion Example:

Inserting an element at index 2 (third position):

index = 2
value = 25
my_array = [10, 20, 30, 40, 50]
my_array.insert(index, value)
print(my_array)  # Outputs: [10, 20, 25, 30, 40, 50]

Why Arrays?

Arrays provide a way to store related data efficiently and access elements quickly by index. They are widely used in many algorithms and fundamental programming constructs.