devDocs
Ask or search…
K
Links

Basic Operations

Arrays are fundamental data structures that allow us to perform various operations efficiently. In this section, we'll explore some of the basic operations on arrays, including:
  1. 1.
    Accessing Elements by Index: Arrays store elements in a contiguous block of memory, and each element has a unique index. Accessing elements by their index allows us to retrieve specific data from the array quickly.
  2. 2.
    Modifying Elements: Arrays are mutable data structures, meaning we can change the values of elements after they are initially assigned.
  3. 3.
    Finding the Length or Size of an Array: Knowing the number of elements in an array is essential for iteration and bounds checking.
  4. 4.
    Iterating Through Arrays: Iteration enables us to process each element in an array, performing operations or calculations as needed.
Let's explore these operations with code examples in C, C++, Java, and Python.
1. Accessing Elements by Index:
Accessing elements in an array is achieved by specifying the index of the desired element within square brackets. Array indices typically start at 0.
// C
int myArray[5] = {10, 20, 30, 40, 50};
int element = myArray[2]; // Accesses the third element (index 2), which is 30.
// C++
int myArray[5] = {10, 20, 30, 40, 50};
int element = myArray[2]; // Accesses the third element (index 2), which is 30.
// Java
int[] myArray = {10, 20, 30, 40, 50};
int element = myArray[2]; // Accesses the third element (index 2), which is 30.
# Python
my_list = [10, 20, 30, 40, 50]
element = my_list[2] # Accesses the third element (index 2), which is 30.
2. Modifying Elements:
You can modify elements in an array by assigning new values to them using their indices.
// C
int myArray[5] = {10, 20, 30, 40, 50};
myArray[2] = 35; // Modifies the third element (index 2) to be 35.
// C++
int myArray[5] = {10, 20, 30, 40, 50};
myArray[2] = 35; // Modifies the third element (index 2) to be 35.
// Java
int[] myArray = {10, 20, 30, 40, 50};
myArray[2] = 35; // Modifies the third element (index 2) to be 35.
# Python
my_list = [10, 20, 30, 40, 50]
my_list[2] = 35 # Modifies the third element (index 2) to be 35.
3. Finding the Length or Size of an Array:
In C and C++, you typically need to manually keep track of the array size. In Java and Python, you can use built-in functions to find the length or size.
// C/C++: No built-in function for array length; manually manage size.
// Java
int[] myArray = {10, 20, 30, 40, 50};
int length = myArray.length; // Retrieves the length, which is 5.
# Python
my_list = [10, 20, 30, 40, 50]
length = len(my_list) # Retrieves the length, which is 5.
4. Iterating Through Arrays:
Iteration is crucial for processing all elements in an array. You can use loops, such as for or while, to iterate through arrays.
// C
int myArray[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
// Access and process each element, e.g., printf("%d\n", myArray[i]);
}
// C++
int myArray[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
// Access and process each element, e.g., cout << myArray[i] << endl;
}
// Java
int[] myArray = {10, 20, 30, 40, 50};
for (int element : myArray) {
// Access and process each element, e.g., System.out.println(element);
}
# Python
my_list = [10, 20, 30, 40, 50]
for element in my_list:
# Access and process each element, e.g., print(element)
These basic operations on arrays form the foundation for more advanced array manipulation techniques and algorithms. They are essential for effectively working with data stored in arrays in various programming languages.