Arrays
Array Operations
Here are some common operations that can be performed on arrays:
1. Accessing Elements
You can access an element in an array using its index. In most programming
languages, indexing starts at 0.
Example (Python):
arr = [10, 20, 30, 40, 50]
print(arr[0]) # Output: 10 (first element)
print(arr[3]) # Output: 40 (fourth element)
2. Inserting Elements
Inserting elements into an array can be done at a specific index. However, in
languages like C, C++, and Java, arrays have a fixed size, so inserting an element
often requires resizing the array or using other data structures (like a list in
Python or ArrayList in Java).
Example (Python, dynamic array):
arr = [10, 20, 30]
arr.insert(1, 15) # Insert 15 at index 1
print(arr) # Output: [10, 15, 20, 30]
3. Deleting Elements
To delete an element, you can remove it by its value or index.
Example (Python):
arr = [10, 20, 30, 40, 50]
arr.remove(30) # Removes the first occurrence of 30
print(arr) # Output: [10, 20, 40, 50]
, In other languages like C++, you may have to shift elements manually after
deletion.
4. Traversing an Array
To access every element in an array, you need to traverse it using a loop.
Example (Python):
arr = [10, 20, 30, 40, 50]
for num in arr:
print(num)
Output:
10
20
30
40
50
5. Searching for an Element
You can search for an element in an array by iterating through all the elements.
Example (Python):
arr = [10, 20, 30, 40, 50]
if 30 in arr:
print("Found 30!")
else:
print("30 not found.")
6. Sorting
Sorting an array arranges its elements in a specified order (ascending or
descending). Many programming languages provide built-in sorting functions.
Example (Python):
Array Operations
Here are some common operations that can be performed on arrays:
1. Accessing Elements
You can access an element in an array using its index. In most programming
languages, indexing starts at 0.
Example (Python):
arr = [10, 20, 30, 40, 50]
print(arr[0]) # Output: 10 (first element)
print(arr[3]) # Output: 40 (fourth element)
2. Inserting Elements
Inserting elements into an array can be done at a specific index. However, in
languages like C, C++, and Java, arrays have a fixed size, so inserting an element
often requires resizing the array or using other data structures (like a list in
Python or ArrayList in Java).
Example (Python, dynamic array):
arr = [10, 20, 30]
arr.insert(1, 15) # Insert 15 at index 1
print(arr) # Output: [10, 15, 20, 30]
3. Deleting Elements
To delete an element, you can remove it by its value or index.
Example (Python):
arr = [10, 20, 30, 40, 50]
arr.remove(30) # Removes the first occurrence of 30
print(arr) # Output: [10, 20, 40, 50]
, In other languages like C++, you may have to shift elements manually after
deletion.
4. Traversing an Array
To access every element in an array, you need to traverse it using a loop.
Example (Python):
arr = [10, 20, 30, 40, 50]
for num in arr:
print(num)
Output:
10
20
30
40
50
5. Searching for an Element
You can search for an element in an array by iterating through all the elements.
Example (Python):
arr = [10, 20, 30, 40, 50]
if 30 in arr:
print("Found 30!")
else:
print("30 not found.")
6. Sorting
Sorting an array arranges its elements in a specified order (ascending or
descending). Many programming languages provide built-in sorting functions.
Example (Python):