Implement Arrays in different Programming Languages Last Updated : 29 Jan, 2024 Comments Improve Suggest changes Like Article Like Report Arrays are one of the basic data structures that should be learnt by every programmer. Arrays stores a collection of elements, each identified by an index or a key. They provide a way to organize and access a fixed-size sequential collection of elements of the same type. In this article, we will learn about the basic use of array in most common languages like C, C++, Java, Python, etc. Table of Content What is an Array?Implementation of array in C++:Implementation of array in C:Implementation of array in Java:Implementation of array in Python:Implementation of array in C#:Implementation of array in JavaScript:Pros and Cons of array:What is an Array?Arrays are data structures that store collections of elements in contiguous memory locations. Each element is identified by an index or key, allowing for efficient random access. Arrays offer advantages like constant-time access and memory efficiency. However, they have disadvantages such as a fixed size and inefficient insertion/deletion. Implementation of array in C++:Declares an integer array arr with a size of 5 and initializes it with values.Accesses the element at index 3 using arr[3] and prints its value.Modifies the element at index 3 by assigning a new value (15) to arr[3] and prints the modified value. C++ #include <iostream> using namespace std; int main() { int arr[5] = {1, 2, 3, 4, 5}; // Initialize an array // Access elements cout << "Element at index 3: " << arr[3] << endl; // Modify elements arr[3] = 15; cout << "Modified element at index 3: " << arr[3] << endl; return 0; } OutputElement at index 3: 4 Modified element at index 3: 15Implementation of array in C:Declares an integer array arr with a size of 5 and initializes it with values.Accesses the element at index 2 using arr[2] and prints its value.Modifies the element at index 2 by assigning a new value (10) to arr[2] and prints the modified value. C #include <stdio.h> int main() { int arr[5] = {1, 2, 3, 4, 5}; // Initializing an array with values // Accessing elements printf("Element at index 2: %d\n", arr[2]); // Modifying elements arr[2] = 10; printf("Modified element at index 2: %d\n", arr[2]); return 0; } OutputElement at index 2: 3 Modified element at index 2: 10Implementation of array in Java:Declares an integer array arr and initializes it with values using the array initializer syntax.Accesses the element at index 4 using arr[4] and prints its value.Modifies the element at index 4 by assigning a new value (20) to arr[4] and prints the modified value. Java public class Main { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; // Initializing an array with values // Accessing elements System.out.println("Element at index 4: " + arr[4]); // Modifying elements arr[4] = 20; System.out.println("Modified element at index 4: " + arr[4]); } } JavaScript class Main { static main() { const arr = [1, 2, 3, 4, 5]; // Initializing an array with values // Accessing elements console.log("Element at index 4: " + arr[4]); // Modifying elements arr[4] = 20; console.log("Modified element at index 4: " + arr[4]); } } // Call the main method Main.main(); OutputElement at index 4: 5 Modified element at index 4: 20Implementation of array in Python:Initializes a Python list arr with values.Accesses the element at index 1 using arr[1] and prints its value.Modifies the element at index 1 by assigning a new value (12) to arr[1] and prints the modified value. Python3 arr = [1, 2, 3, 4, 5] # Initializing an array with values # Accessing elements print("Element at index 1:", arr[1]) # Modifying elements arr[1] = 12 print("Modified element at index 1:", arr[1]) OutputElement at index 1: 2 Modified element at index 1: 12Implementation of array in C#:Declares an integer array arr and initializes it with values using the new keyword.Accesses the element at index 0 using arr[0] and prints its value.Modifies the element at index 0 by assigning a new value (8) to arr[0] and prints the modified value. C# using System; class Program { static void Main() { int[] arr = new int[] {1, 2, 3, 4, 5}; // Initializing an array with values // Accessing elements Console.WriteLine("Element at index 0: " + arr[0]); // Modifying elements arr[0] = 8; Console.WriteLine("Modified element at index 0: " + arr[0]); } } OutputElement at index 0: 1 Modified element at index 0: 8Implementation of array in JavaScript:Initializes a JavaScript array arr with values.Accesses the element at index 0 using arr[0] and prints its value.Modifies the element at index 0 by assigning a new value (9) to arr[0] and prints the modified value. JavaScript let arr = [1, 2, 3, 4, 5]; // Initializing an array with values // Accessing elements console.log("Element at index 0:", arr[0]); // Modifying elements arr[0] = 9; console.log("Modified element at index 0:", arr[0]); OutputElement at index 0: 1 Modified element at index 0: 9Pros and Cons of array:Pros of ArraysCons of Arrays1. Allows direct access using index, facilitating quick retrieval.1. Set size is not easily changeable, limiting adaptability.2. Minimizes memory overhead by storing elements in contiguous memory locations.2. Inefficient when adding/removing elements in the middle.3. Supports easy iteration, simplifying the process with loops.3. Wasted memory if the array is larger than needed.4. Constant-time access provides predictable and consistent performance.4. Elements must be stored in order, restricting memory allocation.5. Facilitates efficient algorithm and data structure implementations.5. Limited flexibility as arrays typically holds one data type. Comment More infoAdvertise with us Next Article Implement Arrays in different Programming Languages vishaldhaygude01 Follow Improve Article Tags : Geeks Premier League DSA Arrays Geeks Premier League 2023 Practice Tags : Arrays Similar Reads DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example 12 min read Like