Implement Arrays in different Programming Languages
Last Updated :
29 Jan, 2024
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.
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: 15
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 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: 10
Implementation 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: 20
Implementation 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: 12
Implementation 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: 8
Implementation 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: 9
Pros and Cons of array:
|
1. 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. |
Similar Reads
Print "GeeksforGeeks" in 10 different programming languages The most elementary part of learning any computer programming language is the ability to print a desired text on the screen or console. Thus, the task of this article is to guide programmers new to any of the 10 different languages discussed below, i.e. GO, Fortran, Pascal, Scala, Perl, ADA, Ruby, K
4 min read
5 Best Languages for Competitive Programming Needless to say, Competitive Programming is one of the most crucial and popular aspects of a programmer's journey. Though, all the programmers are strongly recommended to participate in such coding challenges to enhance their coding skills and to get various ravishing prizes, rewards, and other care
5 min read
Programming Language Generations A computer is a digital machine. It can only understand electric signals either ON or OFF or 1 or 0. But how do we communicate with this digital machine? Just like there are multiple languages we communicate with each other (e.g., English, Hindi, Tamil, Gujarati, etc.). But computers cannot understa
6 min read
How Programming Languages are Changing the World Programming has been revolutionizing the world since the advent of the first software or a code-based project. Programming or coding has opened numerous new ways and paved the way for innovation in almost every industry. Today, with various types of coding languages available and modern tech-powered
6 min read
Top 5 Programming Languages For Ethical Hackers Every tech enthusiast is enthralled by the term Ethical Hacking. A programming language is one of the many skills that an ethical hacker must have. This article will go over the top five programming languages used in ethical hacking. Let's take a look at some programming languages used by ethical ha
11 min read