Explanation of one dimensional array (Data structure)
Explanation of one dimensional array
In this post, we will discuss the concept of “Explanation of one dimensional array”
A one-dimensional Array, often just called an “array” is a linear data structure hat called a fixed number of elements of the same data type. Here’s a deeper dive into how it works and its properties
Structure and storage
- A one-dimensional array is like a list of items arranged in a a single row. it stores data in a contiguous(i.e sequential) block of memory
- Each elements in an array is accessed by its index, an integer that represents the position of the element within the array. the index usually starts at 0(in most programming languages)
Syntax(Example in Different Language)
C/C++: int arr[5]; defines an integer array arr that can hold 5 integer. Python: arr[2,4,6,8,10]
uses a list as an array (Python does not have build in array support but uses lists or libraries like NumPy)
Java: int[] arr=new int[5];
Key properties
Fixed size: The size of array is set when it’s created and cannot be changed (Though in languages like Python, list are more flexible)
Homogeneous elements: All elements in an array must be of the same data type, like integer, floats or characters.
Index Access: Each elements can be accessed directly using its index, making retrieval fast. for example, arr[2] gives the third element.
Operations on Arrays:
Access: Retrieve any element using its index. This operation is o(1) in complexity, meaning it’s very fast.
Update: Replace the value at a specific index, e.g., arr[2]=7
Traversal: iterate through each element in the array.
Search: Find an elements, which could require o(n) , time in the worst case (if you check every element).
Insertion / Deletion: These are generally not done in a one-dimensional array . Since they change its size. How ever , you can replace values(update) or simulate insertion / deletion by shifting elements.
Memory allocation:
In languages like C and C++, arrays are allocated a continuous block of memory For example, an integer array with 5 elements might be allocated memory like this
int arr[10] reserves 10 slots of memory, with each -slot capable of storing an integer(e.g, 4 bytes in many systems)
This contiguity in memory makes arrays efficient in accessing elements since the address of each elements can be calculated using the base address and index.
Use cases
Arrays are ideal for situations where you know the number of elements in advanced and need fast, direct access to each elements.
They are commonly used in tasks that involve repeated calculations or accessing specific items by position, like storing monthly temperatures of scores in a game
Explanation using Different Languages
Example
Detailed Examples across Different Languages
C Language
Declaration: In C , arrays are defined with a fixed size at compile time.
Syntax: Int arr[5]; Initialization: int arr[5]={2,4,6,8,10};
Memory managements: C allocates a continuous block of memory, which means arr[0], arr[1],etc stored one after another.
// single dim array example in c language #include <stdio.h> int main() { int arr[5]={5,10,15,20,25}; //Declare and initialize an array //Access and print elemnts by index for(int i=0; i<5; i++){ printf("Element at index %d: %d\n",i, arr[i]); } return 0; }
When the above code is executed, it produces the following result
Element at index 0: 5 Element at index 1: 10 Element at index 2: 15 Element at index 3: 20 Element at index 4: 25
Explanation
Access: arr[] retrieves the i-th elements
Limitation: You cannot resize an array the size is fixed at creation
C++ Language
C++ arrays work similarly to C arrays but also have the std:: array and std::vector containers in the stranded templates Library (STL) for more flexibility
Syntax: Int arr[5]; (for built in arrays ) or std::array<int, 5> arr; (for std:: array).
Example
#include <iostream> #include <array> int main() { // Declare and initialize an array std::array<int, 5> arr={10,20,30,40,50}; //Access and print elements by index for(int i=0; i<arr.size(); i++){ std::cout << "Elements at index: "<<arr[i]<<std::endl; } return 0; }
When the above code is executed, it produces the following result
Elements at index: 10 Elements at index: 20 Elements at index: 30 Elements at index: 40 Elements at index: 50
Explanation
std::array : Provide array like behaviour but with better type safety and other STL features.
std::vector: A dynamic array that can resize itself, unlike fixed-size array
Python
In Python , arrays are more often represented by list, which can change size and store different types. The array module is used for fixed-type arrays.
# List in Python languages arr=[2,4,6,8,10] #a list in Python acting as an array for i in range(len(arr)): print(arr[i],end=" ")
When the above code is executed, it produces the following result
2 4 6 8 10
JavaScript
JavaScript arrays are dynamic , meaning they can grow or shrink in size
//one -dimentional Array in JavaScriopt let arr=[2,4,6,8,10]; console.log("The elements of an array"); for (let i=0; i<arr.length; i++){ console.log(arr[i]); }
When the above code is executed, it produces the following result
The elements of an array 2 4 6 8 10
C#
In C#, arrays are strongly typed and fixed in size after declaration
// One-Dimesional Array in C# using System; public class OneDimArray { public static void Main(string[] args) { Console.WriteLine ("The elements of an array"); int[] arr={3,6,9,12,15}; for(int i=0; i<arr.Length; i++) { Console.Write (arr[i]+" "); } } }
When the above code is executed, it produces the following result
The elements of an array 3 6 9 12 15
Ruby
In Ruby, arrays are dynamic and support various types
arr=[4,8,12,16,20] arr.each do |element| print "#{ element}" end
When the above code is executed, it produces the following result
4 8 12 16 20
PHP
In PHP, arrays are flexible and can store elements of different types.
<?php // one-dimensional array in PHP $arr=array(1, 2, 3, 4, 5); foreach($arr as $element){ echo $element . " "; } ?>
When the above code is executed, it produces the following result
1 2 3 4 5
Key operations with one-dimensional arrays
- Accessing Elements: You can access any elements using its index. For instance, arr[0] gives the first element.
- Modifying elements: You can assign a new value to an element by referencing its index, like arr[2]=100;
- iterating: Most languages provide loops(for, while, or special constructs like foreach in Java script) to go through each element.
Advantages of one dimensional array
Constant time access: Each element is directly accessible by its index, making retrieval fast
Memory efficiency: Arrays use contiguous memory locations, making data handling efficient
Disadvantages of an one-dim array
Fixed-size: One declared , most arrays can’t be resized. Languages like Python or JavaScript provide dynamic array-like structure(lists)
Inflexibility in type: In strongly typed languages like C, arrays hold only one type of data, meaning you can’t mix integers and string
Summary
A one-dimensional array is a foundational data structure that enables efficient data storage and access. Arrays are key to many algorithm and data processing task, forming the basis for other data structures like matrices(2D arrays), lidts, stacks, and queues.
Suggested for you
Code to read and print integers of an array in Java
Code to read and print strings of an array in Java
Code to read and print characters of an array in Java
Code to read and print integers of an array in C++
Code to read and print strings of an array in C++
Code to read and print characters of an array in C++
Code to read and print integers of an array in C
Code to read and print strings of an array in C
Similar post
One dimensional array in C language
One dimensional array in C++ language
Two dimension array in Java language
Two dimension array in C language
Two dimension array in C++ language
Three dimension array in Java language
Three dimension array in C language
Three dimension array in C++ language
Explanation of two dim array
Explanation of three dim array
Explanation of multi dim array